-1

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)
Megan59781
  • 31
  • 4

1 Answers1

2

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.

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
  • ive done same with activites and it stops working too – Megan59781 Jul 22 '20 at 06:26
  • 1
    @Megan59781 In this case could you add details in the question? *Stop working* doesn't explain your issue. Report the stacktrace also in this case, but first check if you added the Activity in the Manifest. – Gabriele Mariotti Jul 22 '20 at 06:28