Tuesday, April 9, 2013

[android help] Android Lazy loading Images from JSON into gridview


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 {

@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.(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.(ArrayAdapter.java:128)
03-22 14:57:59.286: E/AndroidRuntime(1183): at com.example.staggeredgridviewdemo.StaggeredAdapter.(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


.

stackoverflow.comm

No comments:

Post a Comment

Google Voice on T-Mobile? [General]

Google Voice on T-Mobile? So I recently switched from a GNex on Verizon to a Moto X DE on T-Mobile. I had always used Google Voice for my v...