0

So i am trying to load images from my returned json array into a staggered gridview. I have already tested putting links directly into the array like the example and it works perfectly, but once i try to use json data, i get errors.(Both network, and storage errors) I have all the required permmissions in my manifest(ie, internet, internal and external storage)

Here is my code, can somebody please let me know what's wrong? Thanks!!

package com.example.staggeredgridviewdemo;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;

import com.origamilabs.library.views.StaggeredGridView;

/**
 * 
 * This will not work so great since the heights of the imageViews are
 * calculated on the iamgeLoader callback ruining the offsets. To fix this try
 * to get the (intrinsic) image width and height and set the views height
 * manually. I will look into a fix once I find extra time.
 * 
 * @author Maurycy Wojtowicz
 * 
 */
public class MainActivity extends Activity {

    /*
     * Images are taken by Romain Guy ! He's a great photographer as well as a
     * great programmer. http://www.flickr.com/photos/romainguy
     */

    private String urls[];
    String location = "http://snapoodle.com/APIS/android/feed.php";
    static final String TAG_ITEMS = "print";

    /*
     * private String urls[] = {
     * "http://farm7.staticflickr.com/6101/6853156632_6374976d38_c.jpg",
     * "http://farm8.staticflickr.com/7084/6885444694_6272874cfc.jpg" };
     */
    /**
     * This will not work so great since the heights of the imageViews are
     * calculated on the iamgeLoader callback ruining the offsets. To fix this
     * try to get the (intrinsic) image width and height and set the views
     * height manually. I will look into a fix once I find extra time.
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        StaggeredGridView gridView = (StaggeredGridView) this
                .findViewById(R.id.staggeredGridView1);


        getImages get= (getImages) new getImages();
        get.execute(location);

        //int margin = getResources().getDimensionPixelSize(R.dimen.margin);

        //gridView.setItemMargin(margin); // set the GridView margin

        //gridView.setPadding(margin, 0, margin, 0); // have the margin on the
                                                    // sides as well

        StaggeredAdapter adapter = new StaggeredAdapter(MainActivity.this,
                R.id.imageView1, urls);

        gridView.setAdapter(adapter);
        adapter.notifyDataSetChanged();
    }

    class getImages extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... params) {
            // TODO Auto-generated method stub
            JSONObject json = JSONfunctions
                    .getJSONfromURL(location);

            try {
                JSONArray jarray;
                jarray = json.getJSONArray(TAG_ITEMS);
                urls = new String[jarray.length()];

                for (int i = 0; i < jarray.length(); i++) {
                    JSONObject gridImages = jarray.getJSONObject(i);
                    urls[i] = gridImages.getString("saved_location");
                }

            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


            return null;
        }

    }

}

logcat

    03-22 14:57:59.276: D/AndroidRuntime(1183): Shutting down VM
    03-22 14:57:59.276: W/dalvikvm(1183): threadid=1: thread exiting with uncaught exception (group=0x4111f930)
    03-22 14:57:59.286: E/AndroidRuntime(1183): FATAL EXCEPTION: main
    03-22 14:57:59.286: E/AndroidRuntime(1183): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.staggeredgridviewdemo/com.example.staggeredgridviewdemo.MainActivity}: java.lang.NullPointerException: storage == null
    03-22 14:57:59.286: E/AndroidRuntime(1183):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
    03-22 14:57:59.286: E/AndroidRuntime(1183):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
    03-22 14:57:59.286: E/AndroidRuntime(1183):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
    03-22 14:57:59.286: E/AndroidRuntime(1183):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
    03-22 14:57:59.286: E/AndroidRuntime(1183):     at android.os.Handler.dispatchMessage(Handler.java:99)
    03-22 14:57:59.286: E/AndroidRuntime(1183):     at android.os.Looper.loop(Looper.java:137)
    03-22 14:57:59.286: E/AndroidRuntime(1183):     at android.app.ActivityThread.main(ActivityThread.java:5041)
    03-22 14:57:59.286: E/AndroidRuntime(1183):     at java.lang.reflect.Method.invokeNative(Native Method)
    03-22 14:57:59.286: E/AndroidRuntime(1183):     at java.lang.reflect.Method.invoke(Method.java:511)
    03-22 14:57:59.286: E/AndroidRuntime(1183):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    03-22 14:57:59.286: E/AndroidRuntime(1183):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    03-22 14:57:59.286: E/AndroidRuntime(1183):     at dalvik.system.NativeStart.main(Native Method)
    03-22 14:57:59.286: E/AndroidRuntime(1183): Caused by: java.lang.NullPointerException: storage == null
    03-22 14:57:59.286: E/AndroidRuntime(1183):     at java.util.Arrays$ArrayList.<init>(Arrays.java:38)
    03-22 14:57:59.286: E/AndroidRuntime(1183):     at java.util.Arrays.asList(Arrays.java:154)
    03-22 14:57:59.286: E/AndroidRuntime(1183):     at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:128)
    03-22 14:57:59.286: E/AndroidRuntime(1183):     at com.example.staggeredgridviewdemo.StaggeredAdapter.<init>(StaggeredAdapter.java:20)
    03-22 14:57:59.286: E/AndroidRuntime(1183):     at com.example.staggeredgridviewdemo.MainActivity.onCreate(MainActivity.java:89)
    03-22 14:57:59.286: E/AndroidRuntime(1183):     at android.app.Activity.performCreate(Activity.java:5104)
    03-22 14:57:59.286: E/AndroidRuntime(1183):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
    03-22 14:57:59.286: E/AndroidRuntime(1183):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
    03-22 14:57:59.286: E/AndroidRuntime(1183):     ... 11 more
    03-22 14:57:59.316: D/dalvikvm(1183): GC_CONCURRENT freed 176K, 3% free 8878K/9084K, paused 3ms+1ms, total 19ms
Tunji
  • 73
  • 2
  • 9

4 Answers4

2
 StaggeredAdapter adapter = new StaggeredAdapter(MainActivity.this,
                R.id.imageView1, urls);

        gridView.setAdapter(adapter);
        adapter.notifyDataSetChanged();

add this above code in onPostExecute in AsyncTask

Hope this will help you.

Anujith
  • 9,370
  • 6
  • 33
  • 48
Amol Sawant
  • 13,842
  • 2
  • 20
  • 27
  • @AnmolSawant96Kuli hello please check this : http://stackoverflow.com/questions/23931593/lazy-loading-images-from-json-not-downloaded-in-sd-card – Sophie May 30 '14 at 10:43
0

This line 03-22 14:57:59.286: E/AndroidRuntime(1183): at com.example.staggeredgridviewdemo.MainActivity.onCreate(MainActivity.java:89) says there is some problem in line 89 of your MainActivity. This is where you create the adapter. If you look closely, you'll see that you try to create the adapter with a null list of items - urls is not initialized yet.

The AsyncTask you use is executed in a different thread, so everything that happens in doInBackground (like urls = new String[jarray.length()];) is in another thread. Which, in this case, means that the urls array is initialized after it is used to create the adapter.

To avoid this you can initialize the array before you start the activity. If you don't know it's size at this moment, you can use another adapter, like ListAdapter to initialize it with a non-null list and then fill this list from the activity and call notifyDataSetChanged after the task has finished.

stan0
  • 11,549
  • 6
  • 42
  • 59
  • @stan0 hello, will you help me i am following this tutorial, check this: http://stackoverflow.com/questions/23931593/lazy-loading-images-from-json-not-downloaded-in-sd-card – Sophie May 30 '14 at 10:52
0

Check the data feed, http://snapoodle.com/APIS/android/feed.php , I do not see any data returned from the request.

{"print":[]}

CjD
  • 3
  • 1
0

I was a bit dumb, but I solved it by moving my staggered grid view to onPostExecute and returning urls from do in background

Tunji
  • 73
  • 2
  • 9
  • following your solution but still not resolved please check this: http://stackoverflow.com/questions/23931593/lazy-loading-images-from-json-not-downloaded-in-sd-card – Sophie May 30 '14 at 10:49