1

Hello everyone i want to make this Lazylist dynamic . i have tried with images first buts its coming with force to close . Please guide if my approach is wrong. Here is code

public class Test extends Activity {

    ListView list;
    LazyAdapter adapter;
    private String[] mStrings;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);



        ArrayList<String> listItems = new ArrayList<String>();
        try {
            URL twitter = new URL(
                    "http://midsweden.gofreeserve.com/proj/androidjson.php?identifier=123");
            URLConnection tc = twitter.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    tc.getInputStream()));

            String line;
            while ((line = in.readLine()) != null) {
                JSONArray ja = new JSONArray(line);

                for (int i = 0; i < ja.length(); i++) {
                    JSONObject jo = (JSONObject) ja.get(i);
                    listItems.add(jo.getString("http://midsweden.gofreeserve.com/proj/admin/pictures/file87619.jpg"));
                }
            }
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace(); 
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();

        }

        mStrings = listItems.toArray(new String[listItems.size()]);
         list=(ListView)findViewById(R.id.list);

        adapter=new LazyAdapter(this, mStrings);
         list.setAdapter(adapter);

         Button b=(Button)findViewById(R.id.button1);
         b.setOnClickListener(listener);
        }
    @Override
    public void onDestroy()
    {
        adapter.imageLoader.stopThread();
        list.setAdapter(null);
        super.onDestroy();
    }

    public OnClickListener listener=new OnClickListener(){
        public void onClick(View arg0) {
            adapter.imageLoader.clearCache();
            adapter.notifyDataSetChanged();
        }
    };




}

logcat

07-09 01:00:51.825: WARN/dalvikvm(12674): threadid=3: thread exiting with uncaught exception (group=0x4001e390)
07-09 01:00:51.835: ERROR/AndroidRuntime(12674): Uncaught handler: thread main exiting due to uncaught exception
07-09 01:00:51.865: ERROR/AndroidRuntime(12674): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.droidnova.android.howto.optionmenu/com.droidnova.android.howto.optionmenu.Test}: java.lang.NullPointerException
07-09 01:00:51.865: ERROR/AndroidRuntime(12674):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2596)
07-09 01:00:51.865: ERROR/AndroidRuntime(12674):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2621)
07-09 01:00:51.865: ERROR/AndroidRuntime(12674):     at android.app.ActivityThread.access$2200(ActivityThread.java:126)
07-09 01:00:51.865: ERROR/AndroidRuntime(12674):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1932)
07-09 01:00:51.865: ERROR/AndroidRuntime(12674):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-09 01:00:51.865: ERROR/AndroidRuntime(12674):     at android.os.Looper.loop(Looper.java:123)
07-09 01:00:51.865: ERROR/AndroidRuntime(12674):     at android.app.ActivityThread.main(ActivityThread.java:4595)
07-09 01:00:51.865: ERROR/AndroidRuntime(12674):     at java.lang.reflect.Method.invokeNative(Native Method)
07-09 01:00:51.865: ERROR/AndroidRuntime(12674):     at java.lang.reflect.Method.invoke(Method.java:521)
07-09 01:00:51.865: ERROR/AndroidRuntime(12674):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
07-09 01:00:51.865: ERROR/AndroidRuntime(12674):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
07-09 01:00:51.865: ERROR/AndroidRuntime(12674):     at dalvik.system.NativeStart.main(Native Method)
07-09 01:00:51.865: ERROR/AndroidRuntime(12674): Caused by: java.lang.NullPointerException
07-09 01:00:51.865: ERROR/AndroidRuntime(12674):     at com.droidnova.android.howto.optionmenu.LazyAdapter.getCount(LazyAdapter.java:27)
07-09 01:00:51.865: ERROR/AndroidRuntime(12674):     at android.widget.ListView.setAdapter(ListView.java:431)
07-09 01:00:51.865: ERROR/AndroidRuntime(12674):     at com.droidnova.android.howto.optionmenu.Test.onCreate(Test.java:35)
07-09 01:00:51.865: ERROR/AndroidRuntime(12674):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-09 01:00:51.865: ERROR/AndroidRuntime(12674):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2544)
07-09 01:00:51.865: ERROR/AndroidRuntime(12674):     ... 11 more
07-09 01:00:51.895: INFO/Process(82): Sending signal. PID: 12674 SIG: 3
07-09 01:00:51.895: INFO/dalvikvm(12674): threadid=7: reacting to signal 3
Community
  • 1
  • 1
umar
  • 3,073
  • 5
  • 35
  • 45

3 Answers3

3

Your NullPointer Exception is in getCount() line 27:

Caused by: java.lang.NullPointerException
at com.droidnova.android.howto.optionmenu.LazyAdapter.getCount(LazyAdapter.java:27)

You could fix the sympton by changing getCount() to:

public int getCount() {
    if(data != null){
        return data.length;
    }
    return 0;
}
thaussma
  • 9,756
  • 5
  • 44
  • 46
  • Thanks for your concern . It worked but there is no images list although there is an over here http://midsweden.gofreeserve.com/proj/admin/pictures/file87619.jpg – umar Jul 08 '11 at 23:28
2

Before using LazyAdapter you should have some data to be displayed. I think right now you don't have correct data. I guess mStrings is null. I guess because there's something wrong with your download/parse code. So please debug your parse code to ensure it works fine. Please ensure mStrings has correct data.

Also you don't need to use LazyAdapter. You should have your own adapter that displays your data. But inside your adapter you can use ImageLoader.displayImage() method like I do in LazyAdapter.

Fedor
  • 43,261
  • 10
  • 79
  • 89
  • Thanks a lot for your concern , please can you guide me regarding my this http://stackoverflow.com/questions/6638701/setting-up-dynamic-listactivity/6638767#6638767 . I will looking forward for your advise – umar Jul 09 '11 at 22:24
0
list.setAdapter(adapter);

According to your output, that line is erroring saying that something (your mStrings variable) is null when it tried to getCount() from it.

Try instead moving that line to after you have gathered data on mStrings.

You will want to consider setting it to an empty String[] then having it load the data on a background thread, then adding items through another method at the completion of the download.

Ribose
  • 2,223
  • 16
  • 22
  • exception is no more because of Herrmann help but now i am not getting images . Please check i have updated question for this "Try instead moving that line to after you have gathered data on mStrings " but it doesnt work – umar Jul 08 '11 at 23:39
  • Oh you're doing toArray wrong as well. I will update this answer. EDIT: Nevermind, sorry. I'm wrong... – Ribose Jul 09 '11 at 00:01
  • Please give a look to this thread http://stackoverflow.com/questions/6638701/setting-up-dynamic-listactivity – umar Jul 10 '11 at 02:08