1

I am currently trying to make a Context Menu. I want my context menu to capture the text from the View that spawned the menu. This is the code I am using:

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
    AdapterView.AdapterContextMenuInfo info =
            (AdapterView.AdapterContextMenuInfo) menuInfo;
    selectedOption = ((TextView) info.targetView).getText().toString();
    menu.setHeaderTitle(selectedOption);
}

When executing the app, I am getting the following error:

Caused by: java.lang.NullPointerException: Attempt to read from field 'android.view.View android.widget.AdapterView$AdapterContextMenuInfo.targetView' on a null object reference
        at com.conversions.rolando.conversions.MainActivity.onCreateContextMenu(MainActivity.java:172)

...which points to: selectedOption = ((TextView) info.targetView).getText().toString();

So the error basically means that "info" is null because "menuInfo" is null as well?. I have tried to understand why by reading: What is a NullPointerException, and how do I fix it? but can't seem to find the solution and, or understand the problem.

This is how I'm calling registerForContextMenu():

public void onButtonClickEvent(View sender)
    {
        registerForContextMenu(sender);
        openContextMenu(sender);
        unregisterForContextMenu(sender);
    }

This method is being called by a TextView inside my main.xml

Thank you!

Community
  • 1
  • 1

2 Answers2

0

You seem to understand the problem: info is null, because menuInfo is null. And you are trying to de-reference info. That is, access a field on a null object. Because the object does not exist (ie. is null), the field cannot exist and must not be used. The Java Runtime has detected this de-referencing of null and is throwing the relevant exception, a NullPointerException.

To avoid this error, you should check to see that info is not null, before trying to access its fields, eg:

if (info != null) {
    selectedOption = ((TextView) info.targetView).getText().toString();
    menu.setHeaderTitle(selectedOption);
}
dave
  • 11,641
  • 5
  • 47
  • 65
  • Thanks for your answer dave. I have already added the part you suggested but I can't do anything since info is always null and I can not use "targetview" which is what I really need to make the menu work the way I want. – Rolando Correa Jun 11 '15 at 23:12
  • @RolandoCorrea No problem, happy to help. If info is always null, then it is because menuInfo is always arriving in your method as null. I'm not sure why that would be the case, although BLACKVVINE's answer might help. – dave Jun 11 '15 at 23:26
  • I have a added what BLACKVVINE was suggesting but doesn't fix the problem. – Rolando Correa Jun 11 '15 at 23:46
0

The argument menuInfo is always passed null by android, and that's probably because you should call a certain registerForContextMenu() method explained here

Community
  • 1
  • 1
Iman Akbari
  • 2,167
  • 26
  • 31