显示来自JSON Android的GridView图像

时间:2012-03-30 00:26:02

标签: java android json gridview android-gridview

从JSON向GridView显示图像可以帮助我时遇到问题。

GalleryActivity.java

public class GalleryActivity extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gallery_layout);

         Intent i = getIntent();

         String tempid = i.getStringExtra("tempid");

        GridView gridView = (GridView) findViewById(R.id.grid_view);


        gridView.setAdapter(new ImageAdapter(this,tempid));


        gridView.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> parent, View v,
                    int position, long id) {

            }
        });
    }
}

ImageAdapter.java

public class ImageAdapter extends BaseAdapter {
    private Context mContext;

    public String[] mThumbIds;

    private static String KEY_SUCCESS = "success";

    private static String KEY_IMAGES = "images";
    private static String KEY_IMAGE = "url_img";


    // Constructor
    public ImageAdapter(Context c,String tempID){
        mContext = c;
         final DealerFunctions dealerFunction = new DealerFunctions();
         JSONObject json = dealerFunction.getImages(tempID);

         try {

                if (json.getString(KEY_SUCCESS) != null) {

                    String res = json.getString(KEY_SUCCESS);
                    if (Integer.parseInt(res) == 1) {


                        JSONArray imagesFields = json
                                .getJSONArray(KEY_IMAGES);


                        for (int i = 0; i < imagesFields.length(); i++) {
                            JSONObject x = imagesFields.getJSONObject(i);

                            mThumbIds[i] =  x.getString(KEY_IMAGE);


                        }



                    } else {


                    }

                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

    }


    public int getCount() {
        return mThumbIds.length;
    }


    public Object getItem(int position) {
        return mThumbIds[position];
    }


    public long getItemId(int position) {
        return 0;
    }


    public View getView(int position, View convertView, ViewGroup parent) {         
        ImageView imageView;
        if (convertView==null){

            imageView = new ImageView(mContext);
             imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
             imageView.setLayoutParams(new GridView.LayoutParams(70, 70));

        }else{
            imageView = (ImageView) convertView;
        }

        imageView.setBackgroundDrawable(Drawable.createFromPath(mThumbIds[position]));

        return imageView;
    }

}

不要工作......

当我运行应用程序时,立即关闭假设问题出现在imageView.setBackgroundDrawable(Drawable.createFromPath(mThumbIds [position]));

我需要你帮助的人

Logcat错误如下。

03-27 10:50:12.658: D/AndroidRuntime(1782): Shutting down VM
03-27 10:50:12.658: W/dalvikvm(1782): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
03-27 10:50:12.678: E/AndroidRuntime(1782): FATAL EXCEPTION: main
03-27 10:50:12.678: E/AndroidRuntime(1782): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.montalvo.dealer/com.montalvo.dealer.GalleryActivity}: java.lang.NullPointerException
03-27 10:50:12.678: E/AndroidRuntime(1782):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
03-27 10:50:12.678: E/AndroidRuntime(1782):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
03-27 10:50:12.678: E/AndroidRuntime(1782):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-27 10:50:12.678: E/AndroidRuntime(1782):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-27 10:50:12.678: E/AndroidRuntime(1782):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-27 10:50:12.678: E/AndroidRuntime(1782):     at android.os.Looper.loop(Looper.java:123)
03-27 10:50:12.678: E/AndroidRuntime(1782):     at android.app.ActivityThread.main(ActivityThread.java:4627)
03-27 10:50:12.678: E/AndroidRuntime(1782):     at java.lang.reflect.Method.invokeNative(Native Method)
03-27 10:50:12.678: E/AndroidRuntime(1782):     at java.lang.reflect.Method.invoke(Method.java:521)
03-27 10:50:12.678: E/AndroidRuntime(1782):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-27 10:50:12.678: E/AndroidRuntime(1782):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-27 10:50:12.678: E/AndroidRuntime(1782):     at dalvik.system.NativeStart.main(Native Method)
03-27 10:50:12.678: E/AndroidRuntime(1782): Caused by: java.lang.NullPointerException
03-27 10:50:12.678: E/AndroidRuntime(1782):     at com.montalvo.dealer.ImageAdapter.<init>(ImageAdapter.java:57)
03-27 10:50:12.678: E/AndroidRuntime(1782):     at com.montalvo.dealer.GalleryActivity.onCreate(GalleryActivity.java:25)
03-27 10:50:12.678: E/AndroidRuntime(1782):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-27 10:50:12.678: E/AndroidRuntime(1782):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
03-27 10:50:12.678: E/AndroidRuntime(1782):     ... 11 more
03-27 10:50:15.501: I/Process(1782): Sending signal. PID: 1782 SIG: 9

1 个答案:

答案 0 :(得分:0)

你在这里混合,使用这个

public View getView(int position, View convertView, ViewGroup parent) {         
    ImageView imageView;
    imageView = new ImageView(mContext);
    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
    imageView.setLayoutParams(new GridView.LayoutParams(70, 70));


    imageView.setBackgroundDrawable(Drawable.createFromPath(mThumbIds[position]));

    return imageView;
}

您使用的模式是ViewHolder的模式,但您没有创建任何视图。如果您确实想使用ViewHolder模型,那么

  1. 首先使用ImageView创建一个xml。
  2. 使用ImageView在ImageAdapter中创建一个ViewHolder类。
  3. 在getView()方法中,膨胀xml。
  4. 为convertview == null
  5. 创建视图
  6. 将viewholder设置为convertview的TAG。
  7. 在else condtion中,将先前创建的viewholder分配给convertview。