gpt4 book ai didi

java - 显示来自 JSON Android 的 GridView 图像

转载 作者:行者123 更新时间:2023-12-01 05:24:02 25 4
gpt4 key购买 nike

我在将图像从 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

最佳答案

你在这里混淆了东西,使用这个

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 的模式,但您没有创建任何 View 。如果您确实想使用 ViewHolder 模型,

  1. 首先创建一个带有 ImageView 的 xml。
  2. 在 ImageAdapter 中使用 ImageView 创建 ViewHolder 类。
  3. 在 getView() 方法中,扩充 xml。
  4. 为convertview == null的情况创建viewholder
  5. 将viewholder设置为convertview的TAG。
  6. 在else条件下,将之前创建的viewholder分配给convertview。

关于java - 显示来自 JSON Android 的 GridView 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9935490/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com