2011-09-10 28 views
2

我想以编程方式添加视图到ViewFlipper:创建ImageView的自定义视图,的AsyncTask不工作

flipper.addView(anEpisode(name, null, description), i); 

我的方法是:

public View anEpisode(String n, String i, String d){ 
    View v; 
    LayoutInflater inflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    v = inflater.inflate(R.layout.episodedescription, null); 

    TextView nameOfEpisode = (TextView) v.findViewById(R.id.episodedescriptionname); 
    ImageView imageOfEpisode = (ImageView) v.findViewById(R.id.episodedescriptionimage); 
    TextView descriptionOfEpisode = (TextView) v.findViewById(R.id.episodedescriptiondescription); 

    nameOfEpisode.setText(n); 
    descriptionOfEpisode.setText(d); 
    createUrlImage(imageOfEpisode, i); 

    return v; 
} 

和createUrlImage是:

private class CreateImage extends AsyncTask<String, Void, Drawable> { 
    ImageView image; 
    public CreateImage(ImageView img) { 
     image = img; 
    } 
    protected void onPreExecute() { 
    } 

    protected Drawable doInBackground(String... urls) { 
     InputStream is; 
     Drawable d = null ; 
     try { 
      is = (InputStream)new URL(urls[0]).getContent(); 
      d = Drawable.createFromStream(is, "Image"); 
      return d; 
     } catch (MalformedURLException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     return d; 
    } 
    protected void onPostExecute(Drawable d) { 
     image.setBackgroundDrawable(d); 
    } 
} 
public void createUrlImage(ImageView img, String url){ 
    new CreateImage(img).execute(url); 
} 

事情是:它根本不显示图像,所以我不知道该怎么做。

+0

您可以跟踪您的代码并查看计算机的功能。 – Snicolas

+0

我该怎么做?我只知道如何使用debuger。 – Tsunaze

+0

ResourceType:资源不包含资源编号为0x7f的包....所有页面都有此行。 – Tsunaze

回答

0

我认为问题是可绘制的边界。在onPostExecute()方法中设置边界。

protected void onPostExecute(Drawable d) { 
    d.setBounds (0, 0, getIntrinsicWidth(), getIntrinsicHeight()); 
    image.setBackgroundDrawable(d); 
} 
+0

什么是getIntrinsicWidth和getIntrinsicHeight? – Tsunaze

+0

没问题,这是一种可绘制的方法,但它不起作用,现在它正在放慢应用程序。 – Tsunaze

+0

是的,从drawable的方法。错过了。尝试给出一个静态值来代替'getIntrinsicWidth'和'getIntrinsicHeight'。像'd.setBounds(0,0,100,100);'或者你认为不会超过真实的高度和宽度。 – Ronnie