2011-09-10 26 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