0

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

2 Answers2

1

Sorry, but your code is very bad.

You did a lot of findViewById, which take a long time to operate (VS other methods). With your code, your listview do findViewById for each item.

Use a static holder with custom adapter so you dont to do a lot of findViewById.

I suggest you to spend some time on this tutorial :

http://www.vogella.com/tutorials/AndroidListView/article.html

Or do some googling about how getView and holder work.

If you still insists using your code, post your logcat so i can help you. (but i dont recommend it).

Blaze Tama
  • 10,828
  • 13
  • 69
  • 129
  • thanks for the honest and brutal assessment Blaze Tama, i appreciate it. just wanted to know why it is crashing. please note that my listview is not really that long. it crashes even at less than 10 rows. will check the articles you sent, but will wait for some other responses and input. but still giving you 1 point! :-) – RobEarth Softworks Nov 08 '14 at 07:01
  • 1
    @RobEarthSoftworks yes, i need to do this because if you use that code to real apps with much data, your users gonna cry :) – Blaze Tama Nov 08 '14 at 07:04
  • 1
    @RobEarthSoftworks please post your logcat when the error happens – Blaze Tama Nov 08 '14 at 07:04
  • @ Blaze Tama line 114 is "img_catdes = (ImageView) listView.getChildAt(i).findViewById(R.id.img_catdes);" what confuses me is error only encountered when the listview is becomes longer than the screen's height, or maybe just a coincidence? really appreciate your time responding. – RobEarth Softworks Nov 08 '14 at 07:26
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/64525/discussion-between-blaze-tama-and-robearth-softworks). – Blaze Tama Nov 08 '14 at 07:32
0

Obviously I am still an amateur android developer. Blaze Tama was right that I have a very bad and inefficient code. Found a way better implementation in another stackoverflow article below which solved my problem. Wohoooo!

How can I update a single row in a ListView?

I encountered another issue though by which the checkbox at the bottom of my long ListView (and are not are not visible on the screen unless you scroll down) does not update. Anyway, marking this as answered. :-)

Thanks guys for the interaction!

Community
  • 1
  • 1