gpt4 book ai didi

android - onPostExecute 未被调用

转载 作者:行者123 更新时间:2023-11-29 20:38:14 26 4
gpt4 key购买 nike

我正在尝试获取 onPostExecute()完成 doInBackground() 后调用的方法方法,但它没有被调用。我该如何解决这个问题?

我已经调试过了,可以看到ArrayList<ItemDTO> data里面的数据对象。

感谢任何帮助。

代码

public class GetLLRD {
Context mContext;

public void post_selected(String json, Context context) {
new MyAsyncTask().execute(json);
context = this.mContext;
}

class MyAsyncTask extends AsyncTask<String, Integer, List<ItemDTO>> {


@Override
protected List<ItemDTO> doInBackground(String... params) {

.
.
.
.

Gson gson = new Gson();
Type listType = new TypeToken<List<ItemDTO>>() {
}.getType();
ArrayList<ItemDTO> data = gson.fromJson(sb.toString(), listType);
.
.
.
.


return null;

}

protected void onPostExecute(ArrayList<ItemDTO> result) {

new Handler().postDelayed(new Runnable() {
@Override
public void run() {
new MyAsyncTask().execute();
System.out.println("The method onPostExcute() in GETLLRD class was invoked again");
}
}, 1*30 * 1000);

if (result != null) {
Intent intent = new Intent(mContext, Map.class);
intent.putExtra("list",result);
mContext.startActivity(intent);

}else{
Log.e("123", "Avoiding null pointer, the dat is null in the GETLLRD class!!!");
}

}

}
}

最佳答案

您的 onPostExecute 正在被调用。 但是,正如其他人指出的那样,您在 doInBackground 中返回 null 并且没有将数据传递给你的onPostExecute

public void post_selected(String json, Context context) { 
new MyAsyncTask().execute(json);
context = this.mContext;
}

class MyAsyncTask extends AsyncTask<String, Integer, List<ItemDTO>> {


@Override
protected List<ItemDTO> doInBackground(String... params) {

.
.
.
.

Gson gson = new Gson();
Type listType = new TypeToken<List<ItemDTO>>() {
}.getType();
ArrayList<ItemDTO> data = gson.fromJson(sb.toString(), listType);
.
.
.
.


return data;

}

protected void onPostExecute(ArrayList<ItemDTO> result) {

new Handler().postDelayed(new Runnable() {
@Override
public void run() {
new MyAsyncTask().execute();
System.out.println("The method onPostExcute() in GETLLRD class was invoked again");
}
}, 1*30 * 1000);

if (result != null) {
Intent intent = new Intent(mContext, Map.class);
intent.putExtra("list",result);
mContext.startActivity(intent);

}

}

}

关于android - onPostExecute 未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31245562/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com