Can't Create Handler Inside Thread That Has Not Called Looper.Prepare() Inside Asynctask for Progressdialog

Can't create handler inside thread that has not called Looper.prepare()

You should know that when you try to modify your UI , the only thread who can do that is the UiThread.

So if you want to modify your UI in another thread, try to use the method: Activity.runOnUiThread(new Runnable);

Your code should be like this :

 new Thread() {
public void run() {
YourActivity.this.runOnUiThread(new Runnable(){

@Override
public void run(){
try {
startPayment("Bank");//Edit,integrate this on the runOnUiThread
} catch (Exception e) {
alertDialog.setMessage(e.getMessage());
handler.sendEmptyMessage(1);
progressDialog.cancel();
}
});
}
}
}.start();

Android AsyncTask [Can't create handler inside thread that has not called Looper.prepare()]

You are attempting to update the UI from a background thread. Either move the toast to onPostExecute, which executes on the UI thread (recommended), or call runOnUiThread.

runOnUiThread(new Runnable() {
public void run() {
// runs on UI thread
}
});

Can't create handler inside thread that has not called Looper.prepare() inside AsyncTask for ProgressDialog

The method show() must be called from the User-Interface (UI) thread, while doInBackground() runs on different thread which is the main reason why AsyncTask was designed.

You have to call show() either in onProgressUpdate() or in onPostExecute().

For example:

class ExampleTask extends AsyncTask<String, String, String> {

// Your onPreExecute method.

@Override
protected String doInBackground(String... params) {
// Your code.
if (condition_is_true) {
this.publishProgress("Show the dialog");
}
return "Result";
}

@Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
connectionProgressDialog.dismiss();
downloadSpinnerProgressDialog.show();
}
}

Android : Can't create handler inside thread that has not called Looper.prepare()

You have

readthread = new Thread(new Runnable() {
public void run() {

In the thread's run method you ahve

openprogresdialog();

In openprogresdialog you have a Asynctask and a thread.

Asynctask must be loaded on the ui thread. Check the topic Threading Rules in the link below.

http://developer.android.com/reference/android/os/AsyncTask.html

Can't create handler inside thread that has not called Looper.prepare()

You cannot update ui in a background thread. Ui should be updated on the ui thread. Your Asynctask looks fine but must be loaded on the ui thread.

Also remove the below which is useless. Will also give NullPointerException since progressDialog is not initialized and you call dismiss before initialization

new Thread()
{
@Override
public void run()
{

DialogInterface progressDialog = null;
progressDialog.dismiss(); // even if initialized cannot update ui in a thread
}
}.start();

Can't create handler inside thread that has not called Looper.prepare()

You're calling it from a worker thread. You need to call Toast.makeText() (and most other functions dealing with the UI) from within the main thread. You could use a handler, for example.

Look up Communicating with the UI Thread in the documentation. In a nutshell:

// Set this up in the UI thread.

mHandler = new Handler(Looper.getMainLooper()) {
@Override
public void handleMessage(Message message) {
// This is where you do your work in the UI thread.
// Your worker tells you in the message what to do.
}
};

void workerThread() {
// And this is how you call it from the worker thread:
Message message = mHandler.obtainMessage(command, parameter);
message.sendToTarget();
}

Other options:

You could use Activity.runOnUiThread(). Straightforward if you have an Activity:

@WorkerThread
void workerThread() {
myActivity.runOnUiThread(() -> {
// This is where your UI code goes.
}
}

You could also post to the main looper. This works great if all you have is a Context.

@WorkerThread
void workerThread() {
ContextCompat.getMainExecutor(context).execute(() -> {
// This is where your UI code goes.
}
}

Deprecated:

You could use an AsyncTask, that works well for most things running in the background. It has hooks that you can call to indicate the progress, and when it's done.

It's convenient, but can leak contexts if not used correctly. It's been officially deprecated, and you shouldn't use it anymore.

Android 2.3 Can't create handler inside thread that has not called Looper.prepare() (AsyncTask)

I managed to find a solution, but I do not know if it's the best. From what I googled got this code in my class service FCM:

Handler handler = new Handler(Looper.getMainLooper());
handler.postDelayed(new Runnable() {
@Override
public void run() {
new RegistroTokenTask(getApplicationContext(), refreshedToken, "", SAVE_ABERTURA).execute();
}
}, 2000);

Can't create handler inside thread that has not called Looper.prepare() on some devices

How can you manage the request sent to a handler if your thread is not listening on a looper?

Doc says:
public Handler ()

Added in API level 1
Default constructor associates this handler with the Looper for the current thread. If this thread does not have a looper, this handler won't be able to receive messages so an exception is thrown.

If you want a secondary thread to be able to manage a Handler you must call Looper.prepare()

Example:

public class MyThread extends Thread {
private Handler mHandler;

public void run() {
Looper.prepare();
mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
// manage the message
}
}
};
Looper.loop();
}

public void stopLooper() {
if (Looper.myLooper()!=null)
Looper.myLooper().quitSafely();
}
}

Android AsyncTask Can't create handler inside thread that has not called Looper.prepare()?

Probablly you called AsyncTask.execute() in a background thread.

You can only execute AsyncTask in UI thread.



Related Topics



Leave a reply



Submit