onCreateContextMenu- NPE

onCreateContextMenu- NPE

我正在开发一个代码,在该代码中,长按列表视图时应该会弹出一个上下文菜单 item.This 以前工作过,但不知道为什么它现在是 NPE。请在下面查看我的代码并提出建议。

public void displayImages(){
//some other functionality
 imageAdapter = new GoodAdapter(this, imageBeanArray);
    list.setAdapter(imageAdapter);
    list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
    registerForContextMenu(list);
}

 @Override
public void onCreateContextMenu(ContextMenu menu, View v,
                                ContextMenu.ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    menu.setHeaderTitle("Options");
    menu.add(0, v.getId(), 0, "Rename");
    menu.add(0,v.getId(),0,"Delete");
}

 @Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
    final ImageBean imageBean = imageBeanArray.get(info.position);
    final int selectedImageId= imageBean.getId();

   if(item.getTitle()=="Rename") {//Do some thing}
   else if (item.getTitle()=="Rename") {//Do some thing else}
 }

列表视图的布局声明:

<?xml version="1.0" encoding="utf-8"?>

<ListView
    android:id="@+id/listView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/menu"
    android:background="@color/white"
    android:choiceMode="multipleChoice"
    android:cacheColorHint="#00000000"
    android:scrollbars="none" />

Logcat 说:

E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.kittu.ClientLync, PID: 30281
    java.lang.NullPointerException
            at com.android.internal.policy.impl.PhoneWindow$DecorView.startActionMode(PhoneWindow.java:2694)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.startActionModeForChild(PhoneWindow.java:2619)
            at android.view.ViewGroup.startActionModeForChild(ViewGroup.java:675)
            at android.view.ViewGroup.startActionModeForChild(ViewGroup.java:675)
            at android.view.ViewGroup.startActionModeForChild(ViewGroup.java:675)
            at android.view.View.startActionMode(View.java:4752)
            at android.widget.AbsListView.performLongPress(AbsListView.java:3538)
            at android.widget.AbsListView$CheckForLongPress.run(AbsListView.java:3494)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:146)
            at android.app.ActivityThread.main(ActivityThread.java:5487)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at     com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
            at dalvik.system.NativeStart.main(Native Method)

您似乎无法将 setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL) 与上下文菜单结合使用。由于您的布局 XML 已经(正确地)提供了 android:choiceMode="multipleChoice",您可以安全地删除对 CHOICE_MODE_MULTIPLE_MODAL.

的代码覆盖