gpt4 book ai didi

android - 未在 onPostExecute 上创建 AsyncTask 和 Toast

转载 作者:太空狗 更新时间:2023-10-29 15:54:35 25 4
gpt4 key购买 nike

我有一个 AsyncTask 类。当我有错误时,我想看到一条消息。这是我的 AsynTask 类:

private GetUtenti threadutenti;
threadutenti= (GetUtenti) new GetUtenti(this.getActivity()).execute();

这是我的课:

class GetUtenti extends AsyncTask<Void, Void, Void> {
private Context context;
private boolean errore = false;
private Database db;

public GetUtenti(Context context){
this.context=context;
}
@Override
protected void onPostExecute(Void result) {
db.disconnetti();
if (errore) {
Toast.makeText(context, "error!", Toast.LENGTH_SHORT)
toast.show();
}
else{
setListaUtenti();
}
bProgresso.dismiss();
}
@Override
protected void onPreExecute() {
db = new Database(.....);
}
@Override
protected Void doInBackground(Void... params) {
db.connetti();
if(!db.isConnesso()){
errore= true;
} else {
utenti.clear();
utenti = Utente.getUtenti(db);
}
return null;
}
}

当我想查看我的 Toast 消息时,我有这个:

03-04 17:41:06.534: E/AndroidRuntime(13622): FATAL EXCEPTION: main
03-04 17:41:06.534: E/AndroidRuntime(13622): java.lang.RuntimeException: This Toast was not created with Toast.makeText()
03-04 17:41:06.534: E/AndroidRuntime(13622): at android.widget.Toast.setText(Toast.java:282)
03-04 17:41:06.534: E/AndroidRuntime(13622): at com.unipg.utente.ListUtente$GetUtenti.onPostExecute(ListUtente.java:145)

为什么?

最佳答案

尝试这样的事情:

if (errore) {
Toast toast = Toast.makeText(context, "error!", Toast.LENGTH_SHORT);
toast.show();
}

关于android - 未在 onPostExecute 上创建 AsyncTask 和 Toast,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15206951/

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