出于某种原因,当我长按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的地方,我设置了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);
EditText
框位于ListView
标题中(comment_header
)。不确定这是否重要。
主要问题:为什么要通过长按EditText来注册ContextMenu?
修改/更新 :
我相信CommonsWare现在回答这个问题我正在努力解决它:
请将v与getListView()进行比较,看看它们是否相同 对象与否。如果不是,只需链接到超类。
你是如何实现这一目标的?当我打电话给我时,registerForContextMenu(getListView());
我相信,它会始终根据该代码打开ContextMenu
ListView
,所以我怎么能比较我是否长期按ListView
与EditText
框相比?
答案 0 :(得分:3)
它可以弹出一个ContextMenu对话框来处理EditText
小部件上的长按。通常,此菜单包含以下内容:
由于平台版本的各种差异,以及在各种设备上实施的OEM ui,在EditText上长按时所采取的操作取决于您运行的设备。因此,在不了解您所使用的设备的情况下,我只能大致了解操作系统可以弹出上下文菜单的原因。