【问题标题】:Drawable not displaying in ImageView在 ImageView 中不显示可绘制对象
【发布时间】:2016-01-25 19:09:46
【问题描述】:

您好,我遇到了一个愚蠢的错误,我想我可能遗漏了一些东西。我目前有一个对象列表。对象具有标题、描述和图像。标题和描述正确显示,但由于某种原因图像不会。我在另一个活动中添加了对象并制作了一个列表,当用户选择他们想要查看的项目时,它将 id 发送到它拉出对象的新页面。我在应用程序中添加了一个随机图像作为 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 类:

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;
    }
}

【问题讨论】:

  • 你的 CustomOBJ 类在哪里?
  • 请您发布 CustomOBJ 类代码,看看这里发生了什么:objPic.setImageDrawable(selOBJ.getObjPic());
  • 感谢您的帮助!我重新编辑了我的代码并添加了 customOBJ 类
  • “for 循环”没有用处。直接从第一个活动通过捆绑传递该“x”值,并使用来自第二个活动的“x”从您的列表中获取可绘制的图像
  • @paul590 CustomOBJ 类中没有有效的构造函数。

标签: java android xml android-layout android-relativelayout


【解决方案1】:

发送方:

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 类是否具有有效的构造函数。

也参考这个链接

How to get image from url website in imageview in android

【讨论】:

  • 您好 Lingeshwaran,感谢您为我提供此信息。我对您提供给我的图片链接有疑问,为什么位图比可绘制更好?
  • 它在需要时,据我所知 Drawable 是可以绘制的东西。位图 - 是特定类型的可绘制图像,例如:PNG、JPEG 等
  • 我更新了我的代码以包含位图,但它没有向我显示图像,drawable 的结果相同,会不会是手机上图像的大小搞砸了?
  • 尝试使用您提供的链接中的一个库,效果很好,谢谢谢谢!
猜你喜欢
  • 1970-01-01
  • 2011-05-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-18
相关资源
最近更新 更多