英文:
App crashes when open new activity due to new navigation bar
问题
我的代码在我添加导航栏之前运行正常。现在当我尝试打开新活动时,它崩溃了。我尝试过 finish();
但没有起作用,我认为这是因为我没有原始的重写 public void onCreate
,但我不知道在哪里放置它与导航栏。
这是我的代码:
public class DashboardFragment extends Fragment {
private DashboardViewModel dashboardViewModel;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
dashboardViewModel =
ViewModelProviders.of(this).get(DashboardViewModel.class);
View root = inflater.inflate(R.layout.fragment_dashboard, container, false);
final Button actvitybtn = root.findViewById(R.id.actvityb);
actvitybtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
openActivity1();
int cat = 4;
}
});
return root;
}
public void openActivity1() {
Intent intent = new Intent(getActivity(), Activity1.class);
startActivity(intent);
}
}
我的错误是运行时错误,以下是 Logcat 日志:
07-22 07:22:10.020 1602-1602/com.example.mhtdemo7 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.mhtdemo7, PID: 1602
android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.mhtdemo7/com.example.mhtdemo7.ui.home.HomeFragment}; have you declared this activity in your AndroidManifest.xml?
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1855)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1546)
at android.app.Activity.startActivityForResult(Activity.java:4298)
at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:675)
at androidx.core.app.ActivityCompat.startActivityForResult(ActivityCompat.java:234)
at androidx.fragment.app.FragmentActivity.startActivityFromFragment(FragmentActivity.java:790)
at androidx.fragment.app.FragmentActivity$HostCallbacks.onStartActivityFromFragment(FragmentActivity.java:932)
at androidx.fragment.app.Fragment.startActivity(Fragment.java:1257)
at androidx.fragment.app.Fragment.startActivity(Fragment.java:1245)
at com.example.mhtdemo7.ui.dashboard.DashboardFragment.openActivity1(DashboardFragment.java:47)
at com.example.mhtdemo7.ui.dashboard.DashboardFragment$1.onClick(DashboardFragment.java:38)
at android.view.View.performClick(View.java:5716)
at android.widget.TextView.performClick(TextView.java:10926)
at android.view.View$PerformClick.run(View.java:22596)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7325)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
希望这对你有所帮助。如果你需要进一步的帮助,请随时提问。
英文:
My code worked fine until I put a navigation bar in it. Now it crashes when I try and open a new activity. I've tried finish();
but that didn't work and I think its because I don't have the original override public void onCreate
, but I don't know where I would put that with the navigation bar.
This is my code
public class DashboardFragment extends Fragment {
private DashboardViewModel dashboardViewModel;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
dashboardViewModel =
ViewModelProviders.of(this).get(DashboardViewModel.class);
View root = inflater.inflate(R.layout.fragment_dashboard, container, false);
final Button actvitybtn = root.findViewById(R.id.actvityb);
actvitybtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
openActivity1();
int cat = 4;
}
});
return root;
}
public void openActivity1() {
Intent intent = new Intent(getActivity(), Activity1.class);
startActivity(intent);
}
}
my error is runtime error this is logcat07-22 07:22:10.020 1602-
1602/com.example.mhtdemo7 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.mhtdemo7, PID: 1602
android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.mhtdemo7/com.example.mhtdemo7.ui.home.HomeFragment}; have you declared this activity in your AndroidManifest.xml?
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1855)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1546)
at android.app.Activity.startActivityForResult(Activity.java:4298)
at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:675)
at androidx.core.app.ActivityCompat.startActivityForResult(ActivityCompat.java:234)
at androidx.fragment.app.FragmentActivity.startActivityFromFragment(FragmentActivity.java:790)
at androidx.fragment.app.FragmentActivity$HostCallbacks.onStartActivityFromFragment(FragmentActivity.java:932)
at androidx.fragment.app.Fragment.startActivity(Fragment.java:1257)
at androidx.fragment.app.Fragment.startActivity(Fragment.java:1245)
at com.example.mhtdemo7.ui.dashboard.DashboardFragment.openActivity1(DashboardFragment.java:47)
at com.example.mhtdemo7.ui.dashboard.DashboardFragment$1.onClick(DashboardFragment.java:38)
at android.view.View.performClick(View.java:5716)
at android.widget.TextView.performClick(TextView.java:10926)
at android.view.View$PerformClick.run(View.java:22596)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7325)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
答案1
得分: 2
你正试图使用 startActivity
打开一个 Fragment
:
Intent intent = new Intent(getActivity(), HomeFragment.class);
你不能这样做。这仅适用于活动。
英文:
You are trying to open a Fragment
with startActivity
:
Intent intent = new Intent(getActivity(), HomeFragment.class);
You can't do it. It works with activities.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论