I have a problem that I encountered since I started learning Android.
I have the below code which WORKS PERFECTLY when there are only fewer items in the my ListView:
for (int i = 0; i < listView.getCount(); i++) {
v = listView.getAdapter().getView(i, null, null);
txt_flag = (TextView) v.findViewById(R.id.txt_flag);
flag = txt_flag.getText().toString();
txt_catid = (TextView) v.findViewById(R.id.txt_catid);
catid = Integer.parseInt(txt_catid.getText().toString());
img_catdes = (ImageView) listView.getChildAt(i).findViewById(R.id.img_catdes);
if(categoryid == 1){
img_catdes.setImageResource(R.drawable.category_personal1);
}
else if(categoryid == 2){
img_catdes.setImageResource(R.drawable.category_personal2);
}
if ("2".equals(flag)){
chk_flag = (CheckBox) listView.getChildAt(i).findViewById(R.id.checkBox_completed);
chk_flag.setChecked(true);
}
}
However, when the ListView becomes longer or more specifically, when there are ListView items that are no longer visible in the screen, I always encounter a CRASH!
I am pretty sure that the lines that set the ImageView resource and setting the CheckBox state is the one causing the problem since the error is no longer encountered when I commented that block.
PLEASE HELP guys as this has been giving me difficulty sleeping at night. :-(
Thanks in advance.
LogCat:
11-08 15:16:03.450: D/AbsListView(5306): unregisterIRListener() is called 11-08 15:16:03.470: D/AbsListView(5306): unregisterIRListener() is called 11-08 15:16:03.590: D/dalvikvm(5306): GC_CONCURRENT freed 194K, 25% free 8095K/10684K, paused 3ms+10ms, total 30ms 11-08 15:16:03.640: D/AndroidRuntime(5306): Shutting down VM 11-08 15:16:03.640: W/dalvikvm(5306): threadid=1: thread exiting with uncaught exception (group=0x41c50930) 11-08 15:16:03.640: E/AndroidRuntime(5306): FATAL EXCEPTION: main 11-08 15:16:03.640: E/AndroidRuntime(5306): java.lang.IllegalStateException: Could not execute method of the activity 11-08 15:16:03.640: E/AndroidRuntime(5306): at android.view.View$1.onClick(View.java:3758) 11-08 15:16:03.640: E/AndroidRuntime(5306): at android.view.View.performClick(View.java:4377) 11-08 15:16:03.640: E/AndroidRuntime(5306): at pkg.robearthsoftworks.Main.onWindowFocusChanged(Main.java:89) 11-08 15:16:03.640: E/AndroidRuntime(5306): at com.android.internal.policy.impl.PhoneWindow$DecorView.onWindowFocusChanged(PhoneWindow.java:2825) 11-08 15:16:03.640: E/AndroidRuntime(5306): at android.view.View.dispatchWindowFocusChanged(View.java:7679) 11-08 15:16:03.640: E/AndroidRuntime(5306): at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:971) 11-08 15:16:03.640: E/AndroidRuntime(5306): at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:3328) 11-08 15:16:03.640: E/AndroidRuntime(5306): at android.os.Handler.dispatchMessage(Handler.java:99) 11-08 15:16:03.640: E/AndroidRuntime(5306): at android.os.Looper.loop(Looper.java:137) 11-08 15:16:03.640: E/AndroidRuntime(5306): at android.app.ActivityThread.main(ActivityThread.java:5306) 11-08 15:16:03.640: E/AndroidRuntime(5306): at java.lang.reflect.Method.invokeNative(Native Method) 11-08 15:16:03.640: E/AndroidRuntime(5306): at java.lang.reflect.Method.invoke(Method.java:511) 11-08 15:16:03.640: E/AndroidRuntime(5306): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102) 11-08 15:16:03.640: E/AndroidRuntime(5306): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) 11-08 15:16:03.640: E/AndroidRuntime(5306): at dalvik.system.NativeStart.main(Native Method) 11-08 15:16:03.640: E/AndroidRuntime(5306): Caused by: java.lang.reflect.InvocationTargetException 11-08 15:16:03.640: E/AndroidRuntime(5306): at java.lang.reflect.Method.invokeNative(Native Method) 11-08 15:16:03.640: E/AndroidRuntime(5306): at java.lang.reflect.Method.invoke(Method.java:511) 11-08 15:16:03.640: E/AndroidRuntime(5306): at android.view.View$1.onClick(View.java:3753) 11-08 15:16:03.640: E/AndroidRuntime(5306): ... 14 more 11-08 15:16:03.640: E/AndroidRuntime(5306): Caused by: java.lang.NullPointerException 11-08 15:16:03.640: E/AndroidRuntime(5306): at pkg.robearthsoftworks.Main.updateTestListState(Main.java:114) 11-08 15:16:03.640: E/AndroidRuntime(5306): ... 17 more