0

I am trying to learn java coding and I am building a simple one screen app in Android Studio following a tutorial. The tutorial was written before Jetpack Navigation was introduced and I am running into a compiler error related to Navcontroller code that was inserted. Snippets are listed below. Ideally, I would like to include the basic features of Navcontroller so that I can expand use of those features as I start looking at more complex app structures. So how can I get my simple app to compile and run by resolving the issues below? Or, alternatively, can I comment out or disable the use of Navcontroller as I don't think I need it at this point.

In the code snippet below, I get a compiler error saying it cannot resolve the reference "nav_host_fragment_content_main" in the code line "NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);" in the onCreate routine.

Code snippet:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    binding = ActivityMainBinding.inflate(getLayoutInflater());
    setContentView(binding.getRoot());

    txtGuess = (EditText) findViewById(R.id.txtGuess);
    btnGuess = (Button) findViewById(R.id.btnGuess);
    btnPlayAgain = (Button) findViewById(R.id.btnPlayAgain);
    lblOutput = (TextView) findViewById(R.id.lblOutput);
    newGame();

    btnGuess.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            checkGuess();
        }
    });

    btnPlayAgain.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            btnPlayAgain.setVisibility(View.INVISIBLE);
            txtGuess.setText("");
            newGame();
        }
    });
    setSupportActionBar(binding.toolbar);

    NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
    appBarConfiguration = new AppBarConfiguration.Builder(navController.getGraph()).build();
    NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);

    binding.fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
}
  • Have you read the [Getting Started guide](https://developer.android.com/guide/navigation/navigation-getting-started#navigate)? That specifically tells you what you should be using, particularly in `onCreate()`. – ianhanniballake Apr 04 '23 at 02:44

0 Answers0