我正在尝试修复此代码:
public class Async_Test extends AsyncTask<String, String, String> {
private String Response;
@Override
protected String doInBackground(String... params)
{
String tmp = "something";
return tmp;
}
@Override
protected void onPostExecute(String result) {
Response = result;
}
public String getResponse()
{
return this.Response;
}
}
问题如下:getResponse()方法始终为null。
执行代码:
public void onClick(View v) {
try{
Async_Test t = new Async_Test();
t.execute("");
showMainText("res: "+t.getResponse());
} catch(Exception e)
{
showMainText(e.toString());
}
}
异步任务是异步的。更准确地说,这是调用该execute()
方法时发生的事情:
doInBackground(...)
关于这个新的线程执行doInBackground(...)
Terminate:onPostExecute
在主线程上执行。因此,当您调用showMainText("res: "+t.getResponse());
(它在主线程中)时:onPostExecute
尚未运行,Response
并且仍然为null。
要查看AsyncTask的效果:您可以在行上放置一个断点:
showMainText("res: "+t.getResponse());
并且您将看到显示的文本将是“某物”(由于断点:AsyncTask将有时间执行。)
为了得到你所期望的结果是:招行showMainText("res: "+t.getResponse());
的onPostExecute
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句