android - onCreateContextMenu- NPE -
i developing code in which, context menu should pop on long-clicking list-view item.this worked don't know why npe now. please see code below , suggest.
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 thing} else if (item.gettitle()=="rename") {//do thing else} }
layout declaration of list view:
<?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 says:
e/androidruntime﹕ fatal exception: main process: com.example.kittu.clientlync, pid: 30281 java.lang.nullpointerexception @ com.android.internal.policy.impl.phonewindow$decorview.startactionmode(phonewindow.java:2694) @ com.android.internal.policy.impl.phonewindow$decorview.startactionmodeforchild(phonewindow.java:2619) @ android.view.viewgroup.startactionmodeforchild(viewgroup.java:675) @ android.view.viewgroup.startactionmodeforchild(viewgroup.java:675) @ android.view.viewgroup.startactionmodeforchild(viewgroup.java:675) @ android.view.view.startactionmode(view.java:4752) @ android.widget.abslistview.performlongpress(abslistview.java:3538) @ android.widget.abslistview$checkforlongpress.run(abslistview.java:3494) @ android.os.handler.handlecallback(handler.java:733) @ android.os.handler.dispatchmessage(handler.java:95) @ android.os.looper.loop(looper.java:146) @ android.app.activitythread.main(activitythread.java:5487) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:515) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1283) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:1099) @ dalvik.system.nativestart.main(native method)
it appears cannot use setchoicemode(listview.choice_mode_multiple_modal)
in conjunction context menu. layout xml (correctly) provides android:choicemode="multiplechoice"
, may safely remove code override choice_mode_multiple_modal
.
Comments
Post a Comment