【发布时间】:2015-01-31 02:47:42
【问题描述】:
我有一个单独的类(不是 Activity)来管理带有属性的图像。
在这个类 (Image.class) 中,我还创建了一个 AsynkTask 来从 Web 下载位图。 一切正常,除了 AsyncTask 继续运行,没有触发 onPostExecute() 方法。
有人可以帮帮我吗?
我在 Main Activity 中实例化这个类,在一个循环中。
public class InstaImage {
public URL thumbUrl;
public URL lowresUrl;
public URL standardresUrl;
private URL url;
private Bitmap bmp;
public InstaImage(URL _thumb, URL _low, URL _standard ) {
this.thumbUrl = _thumb;
this.lowresUrl = _low;
this.standardresUrl = _standard;
}
public Bitmap getBitmap(String resolution) {
switch(resolution) {
case "thumb":
url = thumbUrl;
break;
case "low":
url = lowresUrl;
break;
case "standard":
url = standardresUrl;
break;
}
DownloadImageTask dit = new DownloadImageTask();
dit.execute();
// --- TEST ASYNC STATUS ---
while(dit != null) {
if(dit.getStatus() == AsyncTask.Status.FINISHED) {
Log.i("ASYNC", "END2!!");
break;
}
else {
Log.w("ASYNC WAITING", dit.getStatus().toString());
}
}
// --- END TEST ASYNC STATUS ---
return bmp;
}
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
URLConnection conn = null;
InputStream in = null;
Bitmap _bmp = null;
@Override
protected Bitmap doInBackground(String... urls) {
Log.i("ASYNC","STARTING!!");
try {
conn = url.openConnection();
if (conn != null) {
in = conn.getInputStream();
_bmp = BitmapFactory.decodeStream(in);
in.close();
}
}
catch (IOException e) {
e.printStackTrace();
}
Log.i("ASYNC","END0!!");
return _bmp;
}
protected void onPostExecute(Bitmap result) {
super.onPostExecute(result);
Log.i("ASYNC","END1!!");
bmp = result;
}
}
}
【问题讨论】:
-
while(dit != null)-dit怎么会变成空? -
一个强制的 true 参数来保持代码运行.. @michele,你有捕获到异常吗??
-
这非常晦涩难懂。写
while( true ) -
为什么
onPostExecute没有标记为@Override? -
您可能必须让我们知道代码实际输出的是什么,因为您模糊地断言“AsyncTask 继续运行”并不能帮助我们缩小问题范围,或者给我们任何确定性是的。
标签: java android multithreading android-asynctask