user1092042
user1092042

Reputation: 1295

Android Async task not working

I am calling this from a function by creating an object.The progress box appears and then disappears but the toast is not displayed.(I have removed the code that follows the toast but even that code which queries the web page is not executed in the doinBackground)

private class MyTask extends AsyncTask<Void, Void, Void> {

    MyTask(String p) {
        enter=p;
    }

    protected void onPreExecute() {
        progressDialog = ProgressDialog.show(CheckitoutActivity.this,"", "Loading. Please wait...", true);
    }

    protected Void doInBackground(Void... params) {
        try {
            Toast.makeText(getApplicationContext(), "GHJGFHGJL",1000).show();
         }
    }


     @Override
     protected void onPostExecute(Void result) {
         progressDialog.dismiss();
     }

}

The doInBackground does not apppear to be called. Even the toast is not displayed.Why is that.

Upvotes: 0

Views: 316

Answers (3)

Kri
Kri

Reputation: 1856

you can not display the UI like toast and dialog alert in doInBackground method. instead you can show these UI in postExecute method of asynkTask

Upvotes: 2

Samir Mangroliya
Samir Mangroliya

Reputation: 40426

doInBackground method is non UI thread so it can't dispaly a toast

So use runOnUIthread(this) or onProgressUpdate and are sure .execute() with object, like objMytask.execute();

Upvotes: 2

user370305
user370305

Reputation: 109257

Toast (You can't touch UI Thread from doInBack() that's why) is not works in doInBackground() so just put Log

protected Void doInBackground(Void... params) {
 try 
 {
  Log.e("AsyncTask","Inside the doInBackground()");
 }
}

Now check your logcat... If ProgressDailog is showing then your AsyncTask is works well.

Upvotes: 2

Related Questions