I have a listview which have checkboxes. For each checkbox (they are about 3), it has a specific AsyncTask for it.
I never know what checkboxes use
Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
You are trying to display a lertdialog inside doInbackground
. doInbackground
is invoked on a backgroudn thread. and ui should be updated on the ui thread.
You can return result of background computation in doInbackground
and update ui in onPostExecute
. Or use runOnUiThread
which is a method of activity class. or show dialog in onProgressUpdate(Progress...)
http://developer.android.com/reference/android/os/AsyncTask.html
Also check the topic under Threads
@ http://developer.android.com/guide/components/processes-and-threads.html
When an application is launched, the system creates a thread of execution for the application, called "main." This thread is very important because it is in charge of dispatching events to the appropriate user interface widgets, including drawing events. It is also the thread in which your application interacts with components from the Android UI toolkit (components from the android.widget and android.view packages). As such, the main thread is also sometimes called the UI thread.
Also use activity context
alertDialog = new AlertDialog.Builder(ActivityContext).create();
alert dialog is foreground thing so it can not be done in background method of async task. Do it by this way
private class showMessageAsync extends AsyncTask<String, Void, String> {
AlertDialog alertDialog;
protected void onPreExecute() {
super.onPreExecute();
alertDialog = new AlertDialog.Builder(YourClasss.this);
}
@Override
protected String doInBackground(Void... params){
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
alertDialog.setTitle("The Process");
alertDialog.setIcon(R.drawable.success);
alertDialog.setCanceledOnTouchOutside(false);
alertDialog.setMessage("All done!");
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent A = new Intent(DownloadActivity.this, Menu_activity.class);
startActivity(A);
finish();
}
});
alertDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
Intent A = new Intent(DownloadActivity.this, Menu_activity.class);
startActivity(A);
finish();
}
});
alertDialog.show();
}
}
you can access ui by this method inside the function doInBackground... :
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
if (!dialog.isShowing()) {
dialog.show();
}
dialog.setMessage(getText(R.string.lbl_downloading) + " : " + String.valueOf(mCount) + " / " + String.valueOf(totalItems));
}
});
this is a snipped taken from my code which displays the downnloaded items.
you cannot call an alert box inside do inbackground method.same for a toast. No UI operations can be performed inside a doinbg method. instead use the post execute method, or change the way things have to be done.