Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android 2.3.3 AsyncTask call throws NoSuchFieldError

Tags:

android

this works perfectly fine on android 4.0.3 but i get error in android 2.3.3 any help would be greatly appreciated. the line that is bold throws error.

public class TestLoadingTask extends AsyncTask<Object, Object, Void> {
}

btndownload.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        if (util.isNetworkAvailable(NewTakeTest.this)) {
                TestLoadingTask task;
        task = new TestLoadingTask();
        task.currentposition = position;
        task.testname = productsOnCloudList.getList().get(position).getProductname();
        **task.executeOnExecutor(task.THREAD_POOL_EXECUTOR, null);**
    }
    }
});

i am new to stackoverflow so if i am not able to explain my question properly please pardon me.

the entire log is as mentioned below.

09-01 17:07:10.853 E/AndroidRuntime(21188): FATAL EXCEPTION: main
09-01 17:07:10.853 E/AndroidRuntime(21188): java.lang.NoSuchFieldError: in.informationworks.app.CATapp.store.NewTakeTest$TestLoadingTask.THREAD_POOL_EXECUTOR
09-01 17:07:10.853 E/AndroidRuntime(21188): at in.informationworks.app.CATapp.store.NewTakeTest$CloudCustomAdapter$1.onClick(NewTakeTest.java:973)
09-01 17:07:10.853 E/AndroidRuntime(21188): at android.view.View.performClick(View.java:2533)
09-01 17:07:10.853 E/AndroidRuntime(21188): at android.view.View$PerformClick.run(View.java:9320)
09-01 17:07:10.853 E/AndroidRuntime(21188): at android.os.Handler.handleCallback(Handler.java:587)
09-01 17:07:10.853 E/AndroidRuntime(21188): at android.os.Handler.dispatchMessage(Handler.java:92)
09-01 17:07:10.853 E/AndroidRuntime(21188): at android.os.Looper.loop(Looper.java:150)
09-01 17:07:10.853 E/AndroidRuntime(21188): at android.app.ActivityThread.main(ActivityThread.java:4389)
09-01 17:07:10.853 E/AndroidRuntime(21188): at java.lang.reflect.Method.invokeNative(Native Method)
09-01 17:07:10.853 E/AndroidRuntime(21188): at java.lang.reflect.Method.invoke(Method.java:507)
09-01 17:07:10.853 E/AndroidRuntime(21188): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
09-01 17:07:10.853 E/AndroidRuntime(21188): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
09-01 17:07:10.853 E/AndroidRuntime(21188): at dalvik.system.NativeStart.main(Native Method)
09-01 17:07:10.863 E/EmbeddedLogger(  180): App crashed! Process: in.informationworks.app.CATapp
like image 502
user1640445 Avatar asked Sep 01 '12 12:09

user1640445


1 Answers

You have compiled your app using a build target of API Level 11 or higher, but you are running the app on API Level 10 or lower. executeOnExecutor() and THREAD_POOL_EXECUTOR were added in API Level 11 and do not exist on earlier versions of Android. You will need to use android.os.Build to detect what version of Android you are on and use the older execute() method on such devices.

For example:

  @TargetApi(11)
  static public <T> void executeAsyncTask(AsyncTask<T, ?, ?> task,
                                          T... params) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
      task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, params);
    }
    else {
      task.execute(params);
    }
  }

To use this, create your AsyncTask instance, and call executeAsyncTask(), passing the AsyncTask object as the first parameter and whatever you want passed to doInBackground() as the remaining parameters:

fooTask=new FooTask();
executeAsyncTask(fooTask, "these", "are", "optional");
like image 130
CommonsWare Avatar answered Oct 20 '22 10:10

CommonsWare