【发布时间】:2017-03-27 01:10:32
【问题描述】:
我知道以前有人问过这个问题,我已经看过了:
How to get the result of OnPostExecute() to main activity because AsyncTask is a separate class?
how do i send data back from onPostExecute in an AsyncTask?
how to pass the result of asynctask onpostexecute method into the parent activity android
我尝试创建一个接口来传递值,我还尝试创建一个方法来通过我的 onPostExecute 分配值,最后我尝试像下面这样直接设置变量,但似乎没有任何效果。
public class ClassName extends AppCompatActivity {
private Bitmap bit;
public void methodName() {
new RetrieveFavIcon().execute("https://www.google.com");
// work with 'bit' value, but bit value is null, why?
}
class RetrieveFavIcon extends AsyncTask<String, Void, Bitmap> {
public TaskListener delegate = null;
protected Bitmap doInBackground(String... urls) {
try {
URL url = new URL(new URL(urls[0].trim()), "/favicon.ico");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (IOException e) {
Log.d("URL return", "failed");
e.printStackTrace();
return null;
}
}
protected void onPostExecute(Bitmap bm) {
if(bm != null) {
bit = bm;
Log.d("bitmap", "bitmap not null");
}
}
}
【问题讨论】:
-
有什么解释为什么会被否决吗?