Search code examples
androidandroid-asynctaskhandlerandroid-4.0-ice-cream-sandwichandroid-2.3-gingerbread

Asynctask issue on android version < 4


I have developed an app to support android os 4 and above. I have used an Asynctask to make an web service call during app start up.

Later I have changed my minSdkVersion to 9. To support android 2.3 and above. But the same code throws below error.

Calling AsyncTask from SplashActivity thread

  mSplashTread = new Thread() {           @Override           public void run() {
          try {
              int waited = 0;
              while (mActive && (waited < mSplashTime)) {
                  sleep(100);
                  if (mActive) {
                      waited += 100;
                  }
              }
          } catch (InterruptedException e) {
              AppUtil.getInstance().logErrorMessage(TAG, e.getMessage());
          } finally {
              if (mIsBackPressed) {
                  finish();
              } else {
                  GetMobilePremissionWebservice getMobilePermissionWebservice = new GetMobilePremissionWebservice(
                          mContext, SplashActivity.this);
                  getMobilePermissionWebservice.getMobilePermissionInfo();
              }
          }           }       };

Log:

Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() at android.os.Handler.(Handler.java:121) at android.os.AsyncTask$InternalHandler.(AsyncTask.java:421) at android.os.AsyncTask$InternalHandler.(AsyncTask.java:421)

I have checked on the web. Someone saying that the Asynctask internally using the Handler. when it tries to access the UI from doInBackgrond(). But the same code is working fine in os 4 and above. I don't know what is the issue with Asynctask on Android versions 2.3.

Please help me on this.


Solution

  • Actually the problem is I am calling the Asynctask from a thread.

    By using runOnUiThread I am calling Asynctask from an thread is sloves my problem.

    mSplashTread = new Thread() {
                @Override
                public void run() {
                    try {
                        int waited = 0;
                        while (mActive && (waited < mSplashTime)) {
                            sleep(100);
                            if (mActive) {
                                waited += 100;
                            }
                        }
                    } catch (InterruptedException e) {
                        AppUtil.getInstance().logErrorMessage(TAG, e.getMessage());
                    } finally {
                        if (mIsBackPressed) {
                            finish();
                        } else {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    GetMobilePremissionWebservice getMobilePermissionWebservice = new GetMobilePremissionWebservice(
                                            mContext, SplashActivity.this);
                                    getMobilePermissionWebservice
                                            .getMobilePermissionInfo();
                                }
                            });
                        }
                    }
                }
            };
    

    But Still I dont know why the same code working in android version 4.2 Samsung s3.