Android - NullPointerException when calling sendBroadcast(intent)
I am trying to display the options menu when a list item is checked in my app. I am doing this by broadcasting an intent when the checkbox is clicked via a listener and a helper class that开发者_StackOverflow社区 extends Activity. My code for the helper class is:
public class menuHelper extends Activity{
private void showMenu(int checked){
try{
Intent intent = new Intent(SHOW_MENU);
intent.putExtra("check", checked);
sendBroadcast(intent);
}
catch(Exception e){
e.printStackTrace();
}
}
}
The problem is when the sendBroadcast method is invoked. An exception is thrown and the stack trace states that it is a NullPointerException
at sendBroadcast(intent)
. As far as I can tell, the intent is not null, but I am missing something here because the exception is thrown.
It appears that calling the sendBroadcast() method from within an adapter (regardless of using a subclass extending Activity to do so) is simply illegal.
To implement my desired functionality I found that if you inject the calling activity into the adapter class and call a method from the activity to display (or hide) the options menu this null pointer error does not occur and the menu displays or not beautifully.
精彩评论