I am trying to build this app, but Android Studio is throwing me a compile-time error on the file, telling me that statements aren't correctly structured. The first error is an "illegal start of expression" error when I'm trying to declare a method with a public modifier. Then, each object that I use cannot be found, no matter the object (menu, view, toolbar etc).
I can declare objects just fine after I declare my AppBarConfiguration, but I cannot call methods.
I have tried adding private objects before onCreate is called and using those, but the squiggly red line just moves from before the parameters are added to the modifier on the methods.
This is all happening within the onCreate method of my activity. Other apps on my machine find the values and methods declared within onCreate to be just fine and build normally. Here is the code;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuInflater;
import android.view.View;
import android.view.Menu;
import android.widget.Button;
import com.google.android.material.navigation.NavigationView;
import androidx.appcompat.widget.PopupMenu;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
public class MainActivity extends AppCompatActivity {
private AppBarConfiguration mAppBarConfiguration;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Button sdstorage = (Button) findViewById(R.id.goSDCardBtn);
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_home)
.setDrawerLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
//this is where the errors begin.
public void onPop3 (View view){ //illegal start of expression on the blank space right before the View is passed to the method and the view object cannot be found
PopupMenu popThree = new PopupMenu(this, view);
MenuInflater inflater = popThree.getMenuInflater();
inflater.inflate(R.menu.topmenu, popThree.getMenu());
popThree.show();
}
public void goSdCard (View view){illegal start of expression on the blank space right before the View is passed to the method and the view object cannot be found
startActivity(new Intent(MainActivity.this, DirectoryView.class));
}
public void goSettings (View view){ //here too
Navigation.findNavController(view).navigate(R.id.settingsview);
Intent intent = new Intent(this, settings.class);
startActivity(new Intent(MainActivity.this, settings.class));
}
@Override // the Override annotation is throwing an error
public boolean onCreateOptionsMenu (Menu menu){ // a semi-colon is expected before the Menu object is declared
getMenuInflater().inflate(R.menu.main, menu); //
return true;
}
@Override
public boolean onSupportNavigateUp () { // a semi-colon is expected here before and after the parenthesis
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();// everything after the return statement is throwing an error
}
} // this curly brace should signal the end of the onCreate method
}