我有一个应用程序在生产中使用了几个星期,使用 ACRA,在今天报告了一个奇怪的错误之前,我的错误为零。
我有:
    android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
来自堆栈跟踪中的此方法(已追溯):
at my.app.CountdownFragment$1.void onPostExecute(java.lang.Object)(SourceFile:1)
这是相关的源代码片段:
    private void addInstructionsIfNeeded() {
    if (S.sDisplayAssist) {
        new AsyncTask<String, Void, String>() {
            @Override
            protected String doInBackground(String... params) {
                return null;
            }
            /*
             * runs on the ui thread
             */
            protected void onPostExecute(String result) {
                Activity a = getActivity();
                if (S.sHelpEnabled && a != null) {
                    in = new InstructionsView(a.getApplicationContext());
                    RelativeLayout mv = (RelativeLayout) a
                            .findViewById(R.id.main_place);
                    mv.addView(in.prepareView());
                }
            };
        }.execute("");
    }
}
从处理程序调度的消息( UIaddInstructionsIfNeeded()主题)调用的位置。
- onPostExecute()在 UI 线程上运行,为什么我有“错误的线程”?
- 这段代码已经在 150 多台设备上运行了超过 100000 次(根据 Flurry),从未出现过这个错误。
- 原始设备是运行 SDK 4.0.4 的三星 SGH-I997
我的问题是:怎么可能?
编辑:这一切都发生在一个片段中