0

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);

EditTextBox 位于标题ListView( comment_header) 中。不确定这是否重要。

主要问题: 为什么它试图通过长按 EditText 来注册 ContextMenu?

编辑/更新

我相信 CommonsWare 回答了这个问题,现在我正在为它的代码苦苦挣扎:

请比较 v 和 getListView() 以查看它们是否是同一个对象。如果不是,只需链接到超类。

你如何做到这一点?当我调用这个时,registerForContextMenu(getListView());我相信,它总是会根据该代码打开ContextMenuListView所以我如何进行比较是否长按一个ListView与一个EditText框?

4

1 回答 1

3

它可以弹出一个 ContextMenu 对话框来处理EditText小部件上的长按。通常,此菜单将包含以下内容:

  • 复制
  • 粘贴
  • 全选
  • 选择文本

长按 EditText 时采取的操作取决于您正在运行的设备,因为平台版本和 OEM ui 存在各种差异,这些差异在设备范围内实现。因此,在不了解您使用的设备的更多信息的情况下,我只能大致了解操作系统为何弹出上下文菜单。

于 2013-01-26T20:16:13.720 回答