Home > OS >  Check if MenuItem is null
Check if MenuItem is null

Time:03-23

I am trying to make a clean checkbox code in android but makes error if i just press the button and havent checked the checkbox this is the code of cleaning the checkbox:

public void fillDefaultDCN(MenuItem item){
int id = item.getItemId();
if(id == R.id.checkIt){
item.setChecked(false);
checkDCN = false;
}
}

And this is the code i have put on the RESET button

fillDefaultDCN(checkbox);

The checkbox is a declaration:

public MenuItem checkbox;

Apparently the Item.getItemId is null when the checbox is not checked and makes error.

P.S if you dont know the answer or dont understand it better ask or move on then rather give downvote. Let others who can help see the question

CodePudding user response:

Try this

public void fillDefaultDCN(MenuItem item){
if(checkbox == null){
//something
}else{
int id = item.getItemId();
if(id == R.id.checkIt){
item.setChecked(false);
checkDCN = false;
}
}
  • Related