我有以下问题:
我在ListView上显示项目,如果用户点击它,我想显示该项目的ContextMenu。所以我做的是,使用以下代码添加OnItemClickListener:
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (view == null) {
return;
}
if (sessionListView == null) {
sessionListView = (SessionListView) findViewById(R.id.sessionlist);
}
if (view != null) {
sessionListViewSelected = position;
sessionListViewSelectedKey = (String) authList.keySet().toArray()[position];
sessionListView.showContextMenuForChild(view);
}
}
现在,有时我得到以下错误:
FATAL EXCEPTION: main
java.lang.NullPointerException
at android.widget.AdapterView.getPositionForView(AdapterView.java:581)
at android.widget.AbsListView.showContextMenuForChild(AbsListView.java:1926)
at de.trier.infsec.koch.droidsheep.activities.ListenActivity.onItemClick(ListenActivity.java:322)
at android.widget.AdapterView.performItemClick(AdapterView.java:284)
at android.widget.ListView.performItemClick(ListView.java:3513)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:1849)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3835)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
at dalvik.system.NativeStart.main(Native Method)
有人有建议,可能是什么问题?
谢谢!
答案 0 :(得分:0)
可能会出现此问题,因为您正在从单独的线程更新ListAdapter。您应该只改变UI元素(或UI元素所依赖的类)from UI thread。
尝试此操作来更新UI线程上的数据:
listAdapter.getView().post(new Runnable() {
public void run() {
// change listAdapter here
}
});