0

When I run my AsyncTask class on my Samsung galaxy S3(4.1.1) it runs perfectly fine but when I run it on my HTC Android(2.3.4) I get the below error. What is the issue here?

Here is the class.

    public class enemyHandler extends AsyncTask<String, Integer, String>
    {
        @Override
        protected String doInBackground(String... arg0) {
            if(System.currentTimeMillis() - lastSpawn >= 2000)
            {
                Enemy x = new Enemy(SCREEN_WIDTH, SCREEN_HEIGHT, enemy.getHeight());
                enemies.add(x);

                lastSpawn = System.currentTimeMillis();
            }
            for(int i = 0; i < enemies.size(); i++)
            {
                Enemy tempE = enemies.get(i);
                if(tempE.x <= 0 - enemy.getWidth())
                    enemies.remove(tempE);
            }
            return null;
        }
    }

Logcat output:

03-14 18:04:53.656: E/AndroidRuntime(4279): FATAL EXCEPTION: Thread-12
03-14 18:04:53.656: E/AndroidRuntime(4279): java.lang.ExceptionInInitializerError
03-14 18:04:53.656: E/AndroidRuntime(4279):     at com.jister13.plane.Main$SView.run(Main.java:188)
03-14 18:04:53.656: E/AndroidRuntime(4279):     at java.lang.Thread.run(Thread.java:1027)
03-14 18:04:53.656: E/AndroidRuntime(4279): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
03-14 18:04:53.656: E/AndroidRuntime(4279):     at android.os.Handler.<init>(Handler.java:121)
03-14 18:04:53.656: E/AndroidRuntime(4279):     at android.os.AsyncTask$InternalHandler.<init>(AsyncTask.java:421)
03-14 18:04:53.656: E/AndroidRuntime(4279):     at android.os.AsyncTask$InternalHandler.<init>(AsyncTask.java:421)
03-14 18:04:53.656: E/AndroidRuntime(4279):     at android.os.AsyncTask.<clinit>(AsyncTask.java:152)
03-14 18:04:53.656: E/AndroidRuntime(4279):     ... 2 more

SOLUTION:

I was able to solve this problem by calling my

AsyncTask().execute("");

in the onCreate() so as to create the class itself and then recalling it where I wanted it to be run...

Josh
  • 61
  • 9

1 Answers1

0

Something that must get called in the UI thread is not called there. 1. You must call the AsyncTask's execute on the UI thread. 2. Don't handle UI objects on your AsyncTask (views).

Please provide us with more information on the Enemy class (what is it exactly?) and on the code the executing the AsyncTask.

After providing us with the rest of your code: As I said, you do start the AsyncTask from a thread that is not the UI thread. Your SurfaceView implements Runnable, and the part that executing the AsyncTask is in the Thread.run method. In your activity's onCreate you are creating the SurfaceView and in the onResume (called after onCreate), you are starting a new thread (t1), which executing the AsyncTask. To be able to execute this AsyncTask properly, you need to post the code that doing it to the UI thread. AsyncTasks must be started from the UI thread.

Please read about the android.os.Handler class: http://developer.android.com/reference/android/os/Handler.html

And you can see a simple example here: Android: Accessing UI Element from timer thread

I don't know why it's working in 4.1, maybe they did some changes to cover those mistakes of executing AsyncTasks from the UI thread.

Community
  • 1
  • 1
Elad92
  • 2,471
  • 4
  • 22
  • 36
  • Currently my Async task is called from a SurfaceviewClass which is called in onCreate() – Josh Mar 14 '13 at 22:27
  • 1
    @Josh, I'm pretty sure that you start the `AsyncTask` from a thread that is not the UI thread, because you have initialization error. Please post here the code and see this: http://stackoverflow.com/questions/4187960/asynctask-and-looper-prepare-error – Elad92 Mar 14 '13 at 22:38