执行doInBackground()时发生错误

时间:2011-05-23 07:47:35

标签: android

我希望以异步方式从服务器上下载图像,但是当我运行它时强制关闭....并在log cat上发出消息...执行doInBackground时发生错误()...问题是什么请帮助我..请帮助

public class artspacedetailShowingNow extends Activity implements OnClickListener {
    private int imageCounter = 0;

    private ProgressDialog bar;

    private ImageView imageLoader;
    private String[] imageList = {"http://www.artealdiaonline.com/var/artealdia_com/storage/images/argentina/directorio/galerias/ruth_benzacar/artistas/martin_di_girolamo._diosas/198915-1-esl-AR/MARTIN_DI_GIROLAMO._Diosas.jpg","http://www.artealdiaonline.com/var/artealdia_com/storage/images/argentina/directorio/galerias/ruth_benzacar/artistas/jorge_macchi._la_espera/198929-1-esl-AR/JORGE_MACCHI._La_espera.jpg","http://www.artealdiaonline.com/var/artealdia_com/storage/images/argentina/directorio/galerias/ruth_benzacar/artistas/leon_ferrari._hongo_nuclear/198950-1-esl-AR/LEON_FERRARI._Hongo_Nuclear.jpg","http://www.artealdiaonline.com/var/artealdia_com/storage/images/argentina/directorio/galerias/ruth_benzacar/artistas/martin_sastre._fiebre/198922-1-esl-AR/MARTIN_SASTRE._Fiebre.jpg"};
        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.showingnow);
           imageLoader = (ImageView) findViewById(R.id.imageLoader);
        //imageLoader.setImageResource(image1);

        Button next = (Button) findViewById(R.id.next);
        Button back = (Button) findViewById(R.id.back);
        next.setOnClickListener(this);
        back.setOnClickListener(this);
        back.setEnabled(false);

        new ImageDownload().execute(imageList[imageCounter]); 


    }
@Override
 public void onClick(View v)
{
    String imagePath = null;
       // imagePath = imageList[imageCounter];


    }
    new ImageDownload().execute(imagePath);



}
private void loadImage(String imagePath)

{
      try {

          /* Open a new URL and get the InputStream to load data from it. */

          URL aURL = new URL(imagePath);
          URLConnection conn = aURL.openConnection();
          conn.connect();
          InputStream is = conn.getInputStream();

          /* Buffered is always good for a performance plus. */
          BufferedInputStream bis = new BufferedInputStream(is);

          /* Decode url-data to a bitmap. */
          Bitmap bm = BitmapFactory.decodeStream(bis);

          bis.close();
          is.close();
         imageLoader.setImageBitmap(bm);
         imageLoader.setImageBitmap(bm);
      } catch (IOException e) 
      {

          Log.e("DEBUGTAG", "Remote Image Exception", e);

      }

}
private class ImageDownload extends AsyncTask<String , Void, Void>
{
    @Override
    protected Void doInBackground(String... params) {
        loadImage(params[0]);


       return null;
   }


    @Override
    protected void onPostExecute(Void result) {


    }
    @Override
    protected void onPreExecute() {


    }

}
}

3 个答案:

答案 0 :(得分:2)

试试这个

      private class ImageDownload extends AsyncTask<String , Void, Void>
{
    Bitmap imBitmap;
    @Override
    protected Void doInBackground(String... params) {
         try {

              /* Open a new URL and get the InputStream to load data from it. */

              URL aURL = new URL(params[0]);
              URLConnection conn = aURL.openConnection();
              conn.connect();
              InputStream is = conn.getInputStream();

              /* Buffered is always good for a performance plus. */
              BufferedInputStream bis = new BufferedInputStream(is);

              /* Decode url-data to a bitmap. */
              Bitmap bm = BitmapFactory.decodeStream(bis);
              imBitmap=bm;
              bis.close();
              is.close();

          } catch (IOException e) 
          {

              Log.e("DEBUGTAG", "Remote Image Exception", e);

          }


       return null;
   }


    @Override
    protected void onPostExecute(Void result) {
    imageLoader.setImageBitmap(imBitmap);
                 imageLoader.setImageBitmap(imBitmap);

    }
    @Override
    protected void onPreExecute() {


    }

}

你无法在doinBackground中使用imageLoader.setImageBitmap(imBitmap);

答案 1 :(得分:1)

您似乎正在从doInBackground中访问ImageView。这是不允许的。

只能从UI线程处理UI元素。

如果您阅读AsyncTask,您将看到doInBackground在另一个线程中执行,而onPreExecute,onProgressUpdate和onPostExecute在UI线程中执行。

处理我上面提到的方法中的UI元素,或发布像TofferJ建议的可运行的。

答案 2 :(得分:0)

在执行影响UI的操作时,您需要使用UI线程。根据后台线程的起始位置使用:

runOnUiThread(new Runnable() {

    @Override
    public void run() {
        imageLoader.setImageBitmap(bm);
        imageLoader.setImageBitmap(bm);
    }
});

imageLoader.post(new Runnable() {

    @Override
    public void run() {
        imageLoader.setImageBitmap(bm);
        imageLoader.setImageBitmap(bm);
    }
});

上面的两个片段都会确保使用正确的线程来更新您的UI。确保在从另一个步骤修改UI(设置图像,更新文本等)时始终执行此操作,您将避免麻烦。 :)