2

我有一个 AsyncTask 类。当我遇到错误时,我想看到一条 toast 消息。这是我的 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)

为什么?

4

2 回答 2

5

尝试类似:

if (errore) {
    Toast toast = Toast.makeText(context, "error!", Toast.LENGTH_SHORT);
    toast.show();
}
于 2013-03-04T17:05:59.357 回答
3

此行有错误

 Toast.makeText(context, "error!", Toast.LENGTH_SHORT)  no semicolon

无需使用双重步骤,即引用 toast 对象然后显示 . 你可以像这样直接调用

 Toast.makeText(context, "error!", Toast.LENGTH_SHORT).show();
于 2013-03-04T17:09:40.270 回答