fabio
fabio

Reputation: 271

AsyncTask and Toast not created on onPostExecute

i have an AsyncTask class. when i have an error i want to see a toast message. this is my AsynTask class:

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

this is my class:

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;
    }
}

when i want to see my Toast Message i have this:

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)

why?

Upvotes: 2

Views: 2252

Answers (2)

user1780366
user1780366

Reputation:

Error in this line

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

No need to use double steps i.e refering with toast object and then showing . you can call directly like this

 Toast.makeText(context, "error!", Toast.LENGTH_SHORT).show();

Upvotes: 3

Raghav Sood
Raghav Sood

Reputation: 82563

Try something like:

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

Upvotes: 5

Related Questions