应用程序在打开新活动时崩溃,原因是新导航栏。

huangapple go评论93阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2020年7月22日 14:12:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63027999.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定