1

I am trying Navigation Component but I am stuck at this method where IDE shows an error on this method. here's the code where IDE shows error:

import com.google.android.material.bottomnavigation.BottomNavigationView;

    public class MainActivity extends AppCompatActivity {

        @SuppressLint("SourceLockedOrientationActivity")
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

            BottomNavigationView bottomNav = findViewById(R.id.bottomNavigationView);
            NavController navController = findNavController(R.id.fragment);


        }
    }
MMG
  • 3,226
  • 5
  • 16
  • 43
Raunak Pandey
  • 371
  • 2
  • 16

1 Answers1

1

You are searching for navController with R.id.fragment, so make sure this fragment is actually attached to the activity. Even better, call findNavController from the fragment itself

Alex Timonin
  • 1,872
  • 18
  • 31