我試圖在點擊列表視圖中的列表項目時更改圖像。這是脂肪酶說的使用,但它不起作用。該日誌文件指出setimageresource行中存在空指針異常。有任何想法嗎?無法從列表視圖中更改圖像
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
if(arg2 == 0){
image.setImageResource(R.drawable.lighton);
}
else
image.setImageResource(R.drawable.lightoff);
}
});
logcat的
ERROR/AndroidRuntime(712): Uncaught handler: thread main exiting due to uncaught exception
ERROR/AndroidRuntime(712): java.lang.NullPointerException
ERROR/AndroidRuntime(712): at com.Sonny.HCIProject.LightSetting$1.onItemClick(LightSetting.java:42)
ERROR/AndroidRuntime(712): at android.widget.AdapterView.performItemClick(AdapterView.java:283)
ERROR/AndroidRuntime(712): at android.widget.ListView.performItemClick(ListView.java:3132)
ERROR/AndroidRuntime(712): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1620)
ERROR/AndroidRuntime(712): at android.os.Handler.handleCallback(Handler.java:587)
ERROR/AndroidRuntime(712): at android.os.Handler.dispatchMessage(Handler.java:92)
ERROR/AndroidRuntime(712): at android.os.Looper.loop(Looper.java:123)
ERROR/AndroidRuntime(712): at android.app.ActivityThread.main(ActivityThread.java:3948)
ERROR/AndroidRuntime(712): at java.lang.reflect.Method.invokeNative(Native Method)
03-20 03:54:48.702: ERROR/AndroidRuntime(712): at java.lang.reflect.Method.invoke(Method.java:521)
03-20 03:54:48.702: ERROR/AndroidRuntime(712): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
03-20 03:54:48.702: ERROR/AndroidRuntime(712): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
03-20 03:54:48.702: ERROR/AndroidRuntime(712): at dalvik.system.NativeStart.main(Native Method)
的繪製對象文件夾只圖像文件
把你的完整logcat .... – 2012-03-20 03:42:24
如果可能的話給我們展示你的可繪製資源文件夾結構。 – Calvin 2012-03-20 03:44:54
我認爲你沒有正確獲取你的imageview id – 2012-03-20 03:48:30