For some reason, it is trying to bring up a context menu when I long press an EditText box (such as to paste in data).
Here is LogCat:
01-26 12:00:22.243: E/AndroidRuntime(22300): FATAL EXCEPTION: main
01-26 12:00:22.243: E/AndroidRuntime(22300): java.lang.NullPointerException
01-26 12:00:22.243: E/AndroidRuntime(22300): at com.---.---.Comments.onCreateContextMenu(Comments.java:352)
01-26 12:00:22.243: E/AndroidRuntime(22300): at android.view.View.createContextMenu(View.java:7781)
01-26 12:00:22.243: E/AndroidRuntime(22300): at android.view.View.createContextMenu(View.java:7789)
01-26 12:00:22.243: E/AndroidRuntime(22300): at android.view.View.createContextMenu(View.java:7789)
01-26 12:00:22.243: E/AndroidRuntime(22300): at com.android.internal.view.menu.ContextMenuBuilder.show(ContextMenuBuilder.java:81)
01-26 12:00:22.243: E/AndroidRuntime(22300): at com.android.internal.policy.impl.PhoneWindow$DecorView.showContextMenuForChild(PhoneWindow.java:2201)
01-26 12:00:22.243: E/AndroidRuntime(22300): at android.view.ViewGroup.showContextMenuForChild(ViewGroup.java:612)
01-26 12:00:22.243: E/AndroidRuntime(22300): at android.view.ViewGroup.showContextMenuForChild(ViewGroup.java:612)
01-26 12:00:22.243: E/AndroidRuntime(22300): at android.view.ViewGroup.showContextMenuForChild(ViewGroup.java:612)
01-26 12:00:22.243: E/AndroidRuntime(22300): at android.view.ViewGroup.showContextMenuForChild(ViewGroup.java:612)
01-26 12:00:22.243: E/AndroidRuntime(22300): at android.view.ViewGroup.showContextMenuForChild(ViewGroup.java:612)
01-26 12:00:22.243: E/AndroidRuntime(22300): at android.widget.AbsListView.showContextMenuForChild(AbsListView.java:2969)
01-26 12:00:22.243: E/AndroidRuntime(22300): at android.view.ViewGroup.showContextMenuForChild(ViewGroup.java:612)
01-26 12:00:22.243: E/AndroidRuntime(22300): at android.view.View.showContextMenu(View.java:4154)
01-26 12:00:22.243: E/AndroidRuntime(22300): at android.view.View.performLongClick(View.java:4123)
01-26 12:00:22.243: E/AndroidRuntime(22300): at android.widget.TextView.performLongClick(TextView.java:7875)
01-26 12:00:22.243: E/AndroidRuntime(22300): at android.view.View$CheckForLongPress.run(View.java:16945)
01-26 12:00:22.243: E/AndroidRuntime(22300): at android.os.Handler.handleCallback(Handler.java:615)
01-26 12:00:22.243: E/AndroidRuntime(22300): at android.os.Handler.dispatchMessage(Handler.java:92)
01-26 12:00:22.243: E/AndroidRuntime(22300): at android.os.Looper.loop(Looper.java:137)
01-26 12:00:22.243: E/AndroidRuntime(22300): at android.app.ActivityThread.main(ActivityThread.java:4745)
01-26 12:00:22.243: E/AndroidRuntime(22300): at java.lang.reflect.Method.invokeNative(Native Method)
01-26 12:00:22.243: E/AndroidRuntime(22300): at java.lang.reflect.Method.invoke(Method.java:511)
01-26 12:00:22.243: E/AndroidRuntime(22300): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-26 12:00:22.243: E/AndroidRuntime(22300): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-26 12:00:22.243: E/AndroidRuntime(22300): at dalvik.system.NativeStart.main(Native Method)
01-26 12:00:22.250: W/ActivityManager(291): Force finishing activity com.---.rat---/.Comments
The code of my ContextMenu with line causing NullPointer:
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
android.view.MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.comments_context, menu);
menu.setHeaderTitle("Available Actions");
android.view.MenuItem Edit = menu.findItem(R.id.editComment);
android.view.MenuItem Delete = menu.findItem(R.id.deleteComment);
AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
int position = info.position;
ListView lv = (ListView) v;
int firstVisible = lv.getFirstVisiblePosition();
View rowView = lv.getChildAt(position - firstVisible);
// this is the line where there is a null pointer. (Obviousness in an `EditText` box, this `TextView` doesn't exist!
ReviewUser = ((TextView) rowView.findViewById(R.id.labelReviewCommentUser)).getText().toString();
ReviewComment = ((TextView) rowView.findViewById(R.id.labelReviewComment)).getText().toString();
// Other Stuff Edited Out
}
The EditText box is created in my onCreate:
EtComment = (EditText) findViewById(R.id.etTweetComment);
At the very end of onCreate is this:
registerForContextMenu(getListView());
Also, just above where I declare the EditText, I setup listview:
LayoutInflater inflater = this.getLayoutInflater();
ViewGroup header = (ViewGroup) inflater.inflate(
R.layout.comment_header, listView, false);
listView = getListView();
listView.addHeaderView(header, null, false);
listView.setClickable(false);
listView.setTextFilterEnabled(true);
The EditText Box is in the ListView header (comment_header). Not sure if that matters.
Main Question: Why is it trying to register a ContextMenu by longpressing an EditText?
EDIT/UPDATE:
I beleive CommonsWare answers this now I am struggling with code for it:
Please compare v with getListView() to see if they are the same
object or not. If they are not, just chain to the superclass.
How do you achieve this? When I call this, registerForContextMenu(getListView()); I believe, it will ALWAYS open the ContextMenu with the ListView in mind based on that code, so how can I do the comparison whether I am long pressing a ListView vs an EditText box?
It can pop-up a ContextMenu dialog to handle long press on the
EditTextwidget. Generally this menu would contain things like:The action taken when longpressing on the EditText is dependent on the device you are running on due to various differences in the platform versions, and OEM ui’s that get implemented across the range of devices. So without knowing more about what device you are on I can only give a general idea of why the OS could be popping up a context menu.