2013-12-12 141 views
1

我在这里看到了所有类似的问题,因此我在其他活动中使用AsyncTask,但仅在这一个中调用onPostexecute。onPostExecute没有在AsyncTask上调用Android

这里是我的代码后小时oof尝试。

class GetCamera1Task extends AsyncTask<Void, Void, Integer> { 

     private final ProgressDialog dialog = new ProgressDialog(
       CameraGallery.this); 

     @Override 
     protected void onPreExecute() { 
      this.dialog.setMessage("Loading..."); 
      this.dialog.show(); 
     } 

     protected void onPostExecute(int result) { 
      System.out.println("onPostExecute"); 
      this.dialog.dismiss(); 


     } 

     @Override 
     protected Integer doInBackground(Void... params) { 
      // TODO Auto-generated method stub 
      bitmapResult1 = getBitmapFromURL(url[0]); 
      bitmapResult2 = getBitmapFromURL(url[1]); 
     } 
      System.out.println("should return"); 

      return 1; 
     } 

    } 

其中所有变量都是全局变量。 正在打印应返回,但不会调用onPost执行。

我也尽我onBackground返回NULL和sceleton是这样的:

class GetCamera1Task extends AsyncTask<Void, Void, Void> { 
protected void onPostExecute() { 
} 
protected Integer doInBackground(Void... params) { 
... return null; 
} 
} 

却又什么都没有。

下载网址从位图的代码是这样的:

public Bitmap getBitmapFromURL(String src) { 
     try { 
      URL url = new URL(src); 
      HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
      connection.setDoInput(true); 
      connection.connect(); 
      InputStream input = connection.getInputStream(); 
      Bitmap myBitmap = BitmapFactory.decodeStream(input); 
      return myBitmap; 
     } catch (IOException e) { 
      e.printStackTrace(); 
      return null; 
     } 
    } 
+0

同样的问题和答案:请参阅http://stackoverflow.com/questions/6449438/asynctask-onpostexecute-not-being-called – user3080130

回答

6

您所定义的通用参数为AsyncTask<Void, Void, Integer>,但随后在onPostExecute()使用int。试着用

protected void onPostExecute(Integer result) 
0

更换

protected void onPostExecute(int result) 

添加@Override您onPostExecute()方法。它会强调你没有正确地覆盖它。然后将其更改为:

@Override 
protected void onPostExecute(Integer result) 

你应该很好去。

3

在主UI线程上被调用。如果它没有被调用,并且你尝试了上面提到的所有其他技巧,这意味着你的UI线程被阻止做其他事情。

+0

这是2年前回答的。 –

+4

我知道,但它解决我的问题的方式没有在这里提到。添加以便它可以帮助某人。 –

相关问题