2012-04-02 10 views
2

インターネットからダウンロードする画像のリストがあり、各画像の横に説明テキストがリストに表示されています。j2meとlwuitでバックグラウンドで画像をダウンロード

私は今が午前問題は、イメージが私はダウンロードを行うには、スレッドを作成し

をunderstand.`、クラスが以下であることをダウンロードしているとき、アプリがフリーズを取得していることです。 startメソッドを使用してスレッドを開始しますが、runを使用するまで、イメージはNULLになりません。

public class GetImage extends Thread { 

public String imgString; 
private String url; 
private Label label; 
public Image img = null; 

public GetImage(String url, Label label){ 
    this.url = url; 
    this.label = new Label(); 
    this.label = label; 
} 

public Image getPic() 
{ 

    return img; 
} 
public void run() 
{ 
    this.getImage_(); 
    this.label.setIcon(img.scaledHeight(60)); 

} 

public void getImage_() 
{ 
    HttpConnection hc = null; 
    DataInputStream dis = null; 
    DataOutputStream dos = null; 
    StringBuffer messageBuffer = new StringBuffer(); 

    try{ 
     hc = (HttpConnection)Connector.open(this.url,Connector.READ); 
     dis = new DataInputStream(hc.openDataInputStream()); 
     int ch; 
     long len = hc.getLength(); 
     if(len != -1) 
     { 
      for(int i=0; i < len; i++) 
       if((ch = dis.read())!=-1) 
        messageBuffer.append((char)ch); 
     } 
     else 
     { 
      while((ch = dis.read()) != -1) 
       messageBuffer.append((char)ch); 
     } 

     this.img = Image.createImage(messageBuffer.toString().getBytes(),0,messageBuffer.toString().length()); 
     dis.close(); 
    } 
    catch(IOException ae){ 
     messageBuffer.append("Error"); 
    } 
    finally{ 
     try { 
      if (hc != null) hc.close(); 
     } 
     catch (IOException ignored) {} 
     try { 
      if (dis != null) dis.close(); 
     } 
     catch (IOException ignored) {} 
     try { 
      if (dos != null) dos.close(); 
     } 
     catch (IOException ignored) {} 
    } 

    //return this.img; 
} 

} 

とリストを表示するにはlistrenderer:

public class ListRenderer extends Label implements ListCellRenderer { 

public ListRenderer() 
{ 
    super(); 
} 

public Component getListCellRendererComponent(List list, Object o, int i, boolean bln) { 
    //cast the value object into a Content 

    Contents entry = (Contents)o; 
    //get the icon of the Content and set it for this label 
    this.setIcon(entry.getIcon()); 
    //get the text of the Content and set it for this label 
    this.setText(entry.getText()); 
    //set transparency 
    getStyle().setBgTransparency((byte)128); 
    //set background and foreground colors 
    //depending on whether the item is selected or not 
    if(bln) 
    { 
     getStyle().setBgColor(0xffcc33); 
     getStyle().setFgColor(0x000000); 
    } 
    else 
    { 
     getStyle().setBgColor(0xffffff); 

    } 
    return this; 
} 

public Component getListFocusComponent(List list) { 
    setText(""); 
    setIcon(null); 
    getStyle().setBgColor(0xffcc33); 
    getStyle().setBgTransparency(80); 
    return this; 
} 

} 

誰でも助けることができますか?

+0

私が考えることができるのは、スレッドを作成していて、 'start'ではなく' run'を呼び出すかもしれないということだけです。 'start'メソッドは非同期魔法を起こさせます。 – Dylan

+0

いや、私はスタートを使用しますが、画像は使用されるまでヌルになります。 – jade

+0

正しく同期していないため、エラーが発生しています。 [あなたの前の質問への回答で提案されたチュートリアル](http://stackoverflow.com/a/9959124/839601) - これらすべての暗黙の 'synchronized'、' wait'、 'notify'は理由のためにあります – gnat

答えて

0

コメントはおそらく正しいです。このblog postは、あなたが達成しようとしているものと似ているようです。それはCodename Oneにありますが、ほとんどのコードはLWUITにも同様に適用できるはずです。

+0

ありがとう、あなたのコメントは非常に便利だった。私は最終的にlwuit EDTによって公開されたcallSeriallyAndWait()メソッドを使用しました。 – jade

関連する問題