Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Async Task OnProgressUpdate CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views

I am using an AsyncTask to download a database with a progressdialog that shows progress on the UI. Some of my users are receiving the error:

CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

As I understand it, this should only happen if you are trying to update Views off of the UI thread. Here is the error:

com...updateops.DbCreate.onProgressUpdate(DbCreate.java:70) at com...updateops.DbCreate.onProgressUpdate(DbCreate.java:1)

and here is my code:

public class DbCreate extends AsyncTask<String, String, String>{

private static Context mCtx;
private static ProgressDialog mDialog;
public static AmazonSimpleDBClient mSDbClient;
public static AmazonS3Client mS3Client;
private static int mAppVersion;
private static boolean mCreate;


public DbCreate(Context ctx, int versionCode, boolean create) {
    mCtx = ctx.getApplicationContext();
    mAppVersion = versionCode;
    mDialog = new ProgressDialog(ctx);
    mDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    mDialog.setMessage("Checking for server access. Please wait...");
    mDialog.setCancelable(false);
    mDialog.setMax(1);
    mDialog.show(); 
    mCreate = create;

}

protected void onProgressUpdate(String... name) {
    if (name[0].equals("item")) {
        mDialog.incrementProgressBy(1);
    } else if (name[0].equals("setMax")) {
        mDialog.setProgress(0);
        mDialog.setMax(Integer.parseInt(name[1]));   <-- This is line 70
}}


@Override
protected String doInBackground(String... arg0) {
    **do stuff**
    publishProgress("setMax", ""+ 3);
}

It looks to me like I am following exactly what I am supposed to do in order to avoid this error. Anyone know why it's happening?

Edit: I should also mention that this code works most of the time. I am receiving crash reports on the Developer Console.

like image 305
easycheese Avatar asked Mar 22 '23 13:03

easycheese


2 Answers

According to the onProgressUpdate(Progress...) is invoked on the UI thread after a call to publishProgress(Progress...).

You should analyze the whole log report to check if there is any chance that your async task was created on other thread.

And if you really cannot find the root cause you can use a handler created on UI thread to workaround.

like image 77
Robin Avatar answered Apr 25 '23 05:04

Robin


You code looks fine and in most of the cases it should work. I would suggest you to use handler. You can write a handler in UI thread and call it from onProgressUpdate(). This will completely ensure that the UI work is done in UI thread.

This will fix your issue for sure, but I dont know why you are gettin error at first hand. I have seen this kind of issue before and never got a concrete reason for it.

like image 22
Sushil Avatar answered Apr 25 '23 03:04

Sushil