5

我有一个奇怪的错误,似乎只发生在带有 Android 2.2 的三星 Galaxy S 中(我已经在 Motorola Milestone 2 Android 2.2 / Xperia X10 Android 1.6 / Nexus One Android 2.2 / Google G1 Android 1.5 和 1.5 模拟器中测试了该应用程序和在此类设备中不会发生错误)。

完整的堆栈跟踪是:

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

首先,这是怎么回事?主要错误是什么?Can't create handler inside thread that has not called Looper.prepare()还是android.view.InflateException: Binary XML file line #13: Error inflating class <unknown>

当我膨胀View其 XML (R.layout.field_editable_label_value_numeric) 第 13 行是:

        <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" />

编辑:我忘了告诉我正在使用 Proguard,不知道这是否对这个问题有影响(我不这么认为)。

我还在我认为发生问题的地方添加了代码:

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);
            }
        }
}

我的类.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;
4

3 回答 3

8

感谢您的回答,但我得到的最佳解释是在这里:Inflate a view in a background thread

基本上,三星 Galaxy S 似乎有一个EditText实现,其膨胀只能在 UI 线程中完成。

希望这可以帮助遇到同样问题的人。

于 2011-07-14T17:03:31.613 回答
2

不要从后台线程更新您的 UI - 仅使用 UI 线程。

堆栈跟踪看起来像膨胀发生在doInBackgroundAsyncTask 的方法中。大概就是这一行:

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

如果是这种情况,那么您应该将其移出后台线程。如果您发布的代码确实是doInBackground,您可能还需要考虑将您的runOnUiThread块移动到onPostExecute.

于 2011-07-13T23:45:49.153 回答
1

可能与那里类似的问题:LG Optimus w/Swype 上的 EditText 奇怪崩溃(适用于模拟器和 Droids)

似乎在 UI 线程中调用 Looper.prepare() 将修复此异常。

更多信息:http: //developer.android.com/reference/android/os/Looper.html

于 2011-07-13T18:38:24.860 回答