可绘制对象未显示在 ImageView 中

Drawable not displaying in ImageView

您好,我 运行 犯了一个愚蠢的错误,我想我可能遗漏了什么。我目前有一个 object 列表。 object 有标题、描述和图像。标题和描述正确显示,但由于某种原因图像不会。我在另一个 activity 上添加了 objects 并制作了一个列表,当用户选择他们想要查看的项目时,它将 id 发送到它拉取 object 的新页面。我在应用程序中添加了一个随机图像作为 jpg 以查看它是否有脉冲并且确实有脉冲,但它看起来有点奇怪。为什么图像不会从列表中提取的任何想法?提前谢谢你。

XML:

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/img"
    android:src="@drawable/img"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="false"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="News Title"
    android:id="@+id/title"
    android:layout_below="@+id/img"
    android:layout_alignParentLeft="false"
    android:layout_alignParentRight="false"
    android:layout_centerInParent="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Medium Text"
    android:id="@+id/desc"
    android:layout_below="@+id/title"
    android:layout_alignParentStart="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true" />
</RelativeLayout>

JAVA:

CustomOBJ selOBJ = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.det);
    Bundle objID = getIntent().getExtras();
    for(int x = 0; x < MainActivity.listobjs.size(); x++){
        if (MainActivity.listobjs.get(x).getID().equals(objID.getString("object") )){
            selOBJ = MainActivity.listobjs.get(x);
            break;
        }
    }

    TextView Title =(TextView)findViewById(R.id.title);
    TextView description = (TextView)findViewById(R.id.desc);
    ImageView objPic=(ImageView)findViewById(R.id.img);

    Title.setText(selOBJ.getName());
    description.setText(selOBJ.getDescription());
    objPic.setImageDrawable(selOBJ.getObjPic());

}

CustomOBJ Class:

public class CustomOBJ {
private String objID;
private String name;
private String Description;
private Drawable objPic;

public CustomOBJ(String i, String n, String d, Drawable pic) {
    objID = i;
    name = n;
    Description = d;
    objPic = pic;
}

public String getID() { return objID; }
public String getName() {return name;}
public String getDescription() {return description;}
public Drawable getObjPic() {return objPic; }

}

主要活动:

public void jsonTolist(){
    //parse json data
    try{
        jArray = new JSONArray(welcome.result);
        for(int i=0;i<jArray.length();i++){
            JSONObject json_data = jArray.getJSONObject(i);
            Log.i("log_tag", "title:" + json_data.getString("title")

            );



            if(listobjs.isEmpty() || isNewsName(listobjs,json_data) == false){
                Drawable drawable = LoadImageFromWebOperations(json_data.getString("img"));

                selOBJ = new CustomOBJ(json_data.getString("id"), json_data.getString("title"), json_data.getString("desc"), drawable);
                listobjs.add(selOBJ);
            }


        }
    }catch(JSONException e){
        Log.e("log_tag", "Error parsing data " + e.toString());
    }
}

加载图像:

private Drawable LoadImageFromWebOperations(String url){
    try{
        InputStream is = (InputStream) new URL(url).getContent();
        Drawable d = Drawable.createFromStream(is, "src name");
        return d;
    }catch (Exception e) {
        System.out.println("Exc="+e);
        return null;
    }
}

发送方:

Bundle d1 = new Bundle();
d1.putInt("d",0);

接收方:

Bundle objID = getIntent().getExtras();
int x = objID .getInt("d");     

if (MainActivity.listobjs.get(x).getID()
.equals(objID.getString("object") ))
{
        selOBJ = MainActivity.listobjs.get(x);
    }

在执行此操作之前,请检查 CustomOBJ class 是否具有有效的构造函数。

引用此 link 也

How to get image from url website in imageview in android