5

I am having a strange error that seems to occur only in Samsung Galaxy S with Android 2.2 (I've tested the application in Motorola Milestone 2 Android 2.2 / Xperia X10 Android 1.6 / Nexus One Android 2.2 / Google G1 Android 1.5 and 1.5 emulator and in such devices the error does not occur).

The complete stack trace is:

ERROR/AndroidRuntime(28111): FATAL EXCEPTION: AsyncTask #2
ERROR/AndroidRuntime(28111): java.lang.RuntimeException: An error occured while executing doInBackground()
ERROR/AndroidRuntime(28111):     at android.os.AsyncTask$3.done(AsyncTask.java:200)
ERROR/AndroidRuntime(28111):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
ERROR/AndroidRuntime(28111):     at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
ERROR/AndroidRuntime(28111):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
ERROR/AndroidRuntime(28111):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
ERROR/AndroidRuntime(28111):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
ERROR/AndroidRuntime(28111):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
ERROR/AndroidRuntime(28111):     at java.lang.Thread.run(Thread.java:1096)
ERROR/AndroidRuntime(28111): Caused by: android.view.InflateException: Binary XML file line #13: Error inflating class <unknown>
ERROR/AndroidRuntime(28111):     at android.view.LayoutInflater.createView(LayoutInflater.java:513)
ERROR/AndroidRuntime(28111):     at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
ERROR/AndroidRuntime(28111):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
ERROR/AndroidRuntime(28111):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
ERROR/AndroidRuntime(28111):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:621)
ERROR/AndroidRuntime(28111):     at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
ERROR/AndroidRuntime(28111):     at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
ERROR/AndroidRuntime(28111):     at my.package.MyClass.b(Unknown Source)
ERROR/AndroidRuntime(28111):     at my.package.MyClass.a(Unknown Source)
ERROR/AndroidRuntime(28111):     at my.package.MyClass.updateUI(Unknown Source)
ERROR/AndroidRuntime(28111):     at my.package.MyClass.UpdateUITask.doInBackground(Unknown Source)
ERROR/AndroidRuntime(28111):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
ERROR/AndroidRuntime(28111):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
ERROR/AndroidRuntime(28111):     ... 4 more
ERROR/AndroidRuntime(28111): Caused by: java.lang.reflect.InvocationTargetException
ERROR/AndroidRuntime(28111):     at android.widget.EditText.<init>(EditText.java:101)
ERROR/AndroidRuntime(28111):     at java.lang.reflect.Constructor.constructNative(Native Method)
ERROR/AndroidRuntime(28111):     at java.lang.reflect.Constructor.newInstance(Constructor.java:446)
ERROR/AndroidRuntime(28111):     at android.view.LayoutInflater.createView(LayoutInflater.java:500)
ERROR/AndroidRuntime(28111):     ... 16 more
ERROR/AndroidRuntime(28111): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
ERROR/AndroidRuntime(28111):     at android.os.Handler.<init>(Handler.java:121)
ERROR/AndroidRuntime(28111):     at android.view.GestureDetector$GestureHandler.<init>(GestureDetector.java:250)
ERROR/AndroidRuntime(28111):     at android.view.GestureDetector.<init>(GestureDetector.java:370)
ERROR/AndroidRuntime(28111):     at android.view.GestureDetector.<init>(GestureDetector.java:347)
ERROR/AndroidRuntime(28111):     at android.view.GestureDetector.<init>(GestureDetector.java:331)
ERROR/AndroidRuntime(28111):     at android.widget.EditText.<init>(EditText.java:113)
ERROR/AndroidRuntime(28111):     ... 20 more

First of all, what is going on? What is the main error? Can't create handler inside thread that has not called Looper.prepare() or android.view.InflateException: Binary XML file line #13: Error inflating class <unknown> ?

This seems to occur when I am inflating a View whose XML (R.layout.field_editable_label_value_numeric) line 13 is:

        <EditText android:id="@+id/editableLabel"
            android:layout_gravity="left|center_vertical"
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            android:layout_weight="1"
            android:textColor="@color/black"
            android:textStyle="bold" />

EDIT: I forgot to tell that I am using Proguard, don't know if that somehow has influence on this issue (I don't think so).

I also added the code where I think the problem is happening:

MyClass.a:

final LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final ArrayList<View> views = MyClass.b(inflater);

runOnUiThread(new Runnable() {

        @Override
        public void run() {
            LinearLayout mainLayout = (LinearLayout) findViewById(R.id.ItemLinearLayout);

            //clear UI
            mainLayout.removeAllViews();

            //re-set all views
            for (View view : views) {
                mainLayout.addView(view);
            }
        }
}

MyClass.b:

final ArrayList<View> viewsToAdd = new ArrayList<View>();

View view = inflater.inflate(R.layout.field_editable_label_value_numeric, null, true);
viewsToAdd.add(view);

return views;
pandre
  • 6,685
  • 7
  • 42
  • 50

3 Answers3

9

Thanks for the answers, but the best explanation I got was here: Inflate a view in a background thread

Basically, it seems that Samsung Galaxy S has an EditText implementation whose inflation can only be done in the UI Thread.

Hope this helps someone who runs into the same kind of problem.

Community
  • 1
  • 1
pandre
  • 6,685
  • 7
  • 42
  • 50
2

Don't update your UI from a background thread -- use the UI thread only.

The stack trace looks like the inflation is happening in the doInBackground method of an AsyncTask. That's probably this line:

final ArrayList<View> views = MyClass.b(inflater);

If that's the case, then you should move it out of the background thread. If the code you posted is indeed the doInBackground, you may also want to consider moving your runOnUiThread block into onPostExecute.

Mike
  • 2,422
  • 23
  • 14
1

Probably similar problem like there: EditText weird crash on LG Optimus w/Swype (works on emulator and Droids)

It seems that invoking Looper.prepare() in the UI thread will fix this exeption.

More info there: http://developer.android.com/reference/android/os/Looper.html

Community
  • 1
  • 1
pawelzieba
  • 16,082
  • 3
  • 46
  • 72