gho*_*der 1 java android android-asynctask
我在SO上看到了所有类似的问题,我在其他活动中使用了AsyncTask,但只有这一个没有调用onPostexecute.
这是我的代码在几小时后尝试.
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;
}
}
Run Code Online (Sandbox Code Playgroud)
其中所有变量都是全局变量.正在打印应该返回但是没有调用onPost执行.
我也试过我的onBackground返回null和sceleton如下:
class GetCamera1Task extends AsyncTask<Void, Void, Void> {
protected void onPostExecute() {
}
protected Integer doInBackground(Void... params) {
... return null;
}
}
Run Code Online (Sandbox Code Playgroud)
但又一无所获.
从url下载bitMap的代码是这样的:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
您所定义的通用参数AsyncTask<Void, Void, Integer>,但随后用int在onPostExecute().尝试更换
protected void onPostExecute(int result)
Run Code Online (Sandbox Code Playgroud)
同
protected void onPostExecute(Integer result)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6083 次 |
| 最近记录: |