I'm trying to make a toast on item selected but it's showing unreachable statement error.
My code is:
btn=(Button)findViewById(R.id.button);
btn.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
btn.performLongClick();
}
}
);
registerForContextMenu(btn);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("For Optimum Results");
menu.add(0,v.getId(),0,"Hi");
menu.add(0,v.getId(),0,"Hello");
}
@Override
public boolean onContextItemSelected(MenuItem item) {
return super.onContextItemSelected(item);
if(item.getTitle()=="Hi"){
Toast.makeText(this,"hi",Toast.LENGTH_SHORT).show();
}
return true;
}
you have two type of error:
1)return super (return super.onContextItemSelected(item)
) , it means that you return your method at first line so lines below not execute.
2)wrong comparing String (item.getTitle()=="Hi"
)
correct code should be like this:
@Override
public boolean onContextItemSelected(MenuItem item) {
//return super.onContextItemSelected(item);//remove this line
if(item.getTitle().equal("Hi")){// also maybe you want to check not null for item.getTitle()
Toast.makeText(this,"hi",Toast.LENGTH_SHORT).show();
}
return true;
}