【问题】
之前已经遇到过一次,TabHost在addTab时挂掉:
现在又遇到了:
1 | mTabHost.addTab(singleTab); |
时挂掉:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | 03-25 16:09:05.168: W /dalvikvm (12010): threadid=12: thread exiting with uncaught exception (group=0x41abce10) 03-25 16:09:05.198: E /AndroidRuntime (12010): FATAL EXCEPTION: pool-1-thread-1 03-25 16:09:05.198: E /AndroidRuntime (12010): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. 03-25 16:09:05.198: E /AndroidRuntime (12010): at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:5385) 03-25 16:09:05.198: E /AndroidRuntime (12010): at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:1050) 03-25 16:09:05.198: E /AndroidRuntime (12010): at android.view.View.requestLayout(View.java:16150) 03-25 16:09:05.198: E /AndroidRuntime (12010): at android.view.View.requestLayout(View.java:16150) 03-25 16:09:05.198: E /AndroidRuntime (12010): at android.view.View.requestLayout(View.java:16150) 03-25 16:09:05.198: E /AndroidRuntime (12010): at android.view.View.requestLayout(View.java:16150) 03-25 16:09:05.198: E /AndroidRuntime (12010): at android.view.View.requestLayout(View.java:16150) 03-25 16:09:05.198: E /AndroidRuntime (12010): at android.view.View.requestLayout(View.java:16150) 03-25 16:09:05.198: E /AndroidRuntime (12010): at android.view.ViewGroup.addView(ViewGroup.java:3325) 03-25 16:09:05.198: E /AndroidRuntime (12010): at android.view.ViewGroup.addView(ViewGroup.java:3272) 03-25 16:09:05.198: E /AndroidRuntime (12010): at android.view.ViewGroup.addView(ViewGroup.java:3248) 03-25 16:09:05.198: E /AndroidRuntime (12010): at android.widget.TabWidget.addView(TabWidget.java:497) 03-25 16:09:05.198: E /AndroidRuntime (12010): at android.widget.TabHost.addTab(TabHost.java:253) 03-25 16:09:05.198: E /AndroidRuntime (12010): at xxx.yyy.zzz.aaa.MainActivity.onEvent(MainActivity.java:486) 03-25 16:09:05.198: E /AndroidRuntime (12010): at xxx.yyy.zzz.bb.EventCenter.Subject.publish(Subject.java:17) 03-25 16:09:05.198: E /AndroidRuntime (12010): at xxx.yyy.zzz.ccc.AsyncActions.InitDeviceModelAction.Execute(InitDeviceModelAction.java:31) 03-25 16:09:05.198: E /AndroidRuntime (12010): at xxx.yyy.zzz.bbb.ActionExecutor.ActionBase.run(ActionBase.java:66) 03-25 16:09:05.198: E /AndroidRuntime (12010): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 03-25 16:09:05.198: E /AndroidRuntime (12010): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) 03-25 16:09:05.198: E /AndroidRuntime (12010): at java.lang.Thread.run(Thread.java:856) |
【解决过程】
1.后来自己摸索,改为:
(1)把之前的generateMainTabUI,去掉Thread
1 2 3 4 5 6 7 8 9 10 11 12 | private void generateMainTabUI(){ // Thread thread = new Thread(new Runnable() { // // @Override // public void run() { ...... mTabHost.addTab(singleTab); } // }}); // // thread.start(); } |
(2)mHandler中正常去调用generateMainTabUI
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | @SuppressLint ( "HandlerLeak" ) private Handler mHandler = new Handler() { public void handleMessage(Message msg) { switch (msg.what) { ...... case MESSAGE_GEN_MAIN_TAB_UI: if ( null != msg){ generateMainTabUI(); } break ; ...... }; }; |
(3)onEvent中正常去发message,以使得后续可以正常自行到上面的handler
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | @Override public void onEvent(String eventName, Object data, IEventResult result) { if (eventName.equals(EventName.BLLInitDeviceModelComplete)) { ...... if (result.IsSuccess()){ ...... Message msg = new Message(); msg.what = MESSAGE_GEN_MAIN_TAB_UI; msg.obj=deviceModel; mHandler.sendMessage(msg); } else { ...... } } ...... } |
【总结】
大概的感觉是:
Handler中,本身是其他线程,不能再次new线程了,否则去处理UI的事情的时候,会由于新new出来的线程,不是原先的UI的线程,没有所属权,所以会导致挂掉。
转载请注明:在路上 » 【已解决】Android中再次遇到TabHost在addTab时挂掉:android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.