In my code, when an activity is opened/started, I am trying to check off one of the radiobuttons in the menu. I have a function in the onCreate method, but I am getting an error saying "int cannot be dereferenced." How do I fix it in this context? Or, is there a completely different way to go about my goal that would work?
onCreate method
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_markets);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
R.id.servemenu.setChecked(true); //this what what I am using to check the radio button
//home button
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(ActivityMarkets.this, ActivityMenu.class);
startActivity(intent);
}
});
//Don't know what this is for but I'm not gonna mess with it
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
//Menu I think
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
.xml menu code
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="@+id/servemenu"
android:title="Markets We Serve" />
<item
android:id="@+id/federalmenu"
android:title="Federal" />
<item
android:id="@+id/statelocalmenu"
android:title="State and Local" />
<item
android:id="@+id/commercialmenu"
android:title="Commercial" />
</group>
You cannot directly reference the button the way you are doing it.
R.id.servemenu
is just an integer which identifies the radiobutton within the view. You must retrieve the radiobutton by using R.id.servemenu
, if that is indeed the ID of your RadioButton.
You should do it like this,
RadioButton rb = (RadioButton) findViewById(R.id.servemenu);
rb.setChecked(true);