EditText
出于某种原因,当我长按一个框(例如粘贴数据) 时,它试图调出一个上下文菜单。
这是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
我的 ContextMenu 的代码带有导致 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
}
EditText 框是在我的onCreate
:
EtComment = (EditText) findViewById(R.id.etTweetComment);
最后onCreate
是这样的:
registerForContextMenu(getListView());
此外,在我声明 EditText 的上方,我设置了列表视图:
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);
EditText
Box 位于标题ListView
( comment_header
) 中。不确定这是否重要。
主要问题: 为什么它试图通过长按 EditText 来注册 ContextMenu?
编辑/更新:
我相信 CommonsWare 回答了这个问题,现在我正在为它的代码苦苦挣扎:
请比较 v 和 getListView() 以查看它们是否是同一个对象。如果不是,只需链接到超类。
你如何做到这一点?当我调用这个时,registerForContextMenu(getListView());
我相信,它总是会根据该代码打开ContextMenu
,ListView
所以我如何进行比较是否长按一个ListView
与一个EditText
框?