Warning dialog inside asynctask in android

I need to show an Alertdialog inside Asynctask in android, but it will not show in throws exception.

 05-13 14:59:35.522: WARN/System.err(1179): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() 

Thanks.

+6
source share
5 answers

Unable to manage user interface from background thread. To do this, use the handler or the onPre / Post methods for AsyncTask.

+12
source

Show the dialog in the onPostExecute () method, not doInBackground.

Hope that helps

+3
source

I think this may be your problem - you cannot show the dialog from inside doInBackground.

+2
source

There is no problem reaching the user interface thread from backGround thread (more clearly: from the doInBackground () method): you just need to run the runOnUiThread() method in your context. In my case, I call it from the body of doInBackground() .

 ContactsViewerActivity.this.runOnUiThread(new Runnable() { @Override public void run() { AlertDialog.Builder builder = new AlertDialog.Builder(ContactsViewerActivity.this); builder.setTitle("Connection problem..") .setMessage("Could not connect to " + url + ", " + "try changing your host in the settings to default one :" + getResources().getString(R.string.default_server_ip) + ":" + getResources().getString(R.string.default_server_port)) .setNegativeButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }); AlertDialog alert = builder.create(); alert.show(); } }); 
+2
source

You can call publishProgress () directly from doInBackground () and manipulate the user interface in onProgressUpdate ().

0
source

Source: https://habr.com/ru/post/888022/


All Articles