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!