最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】Android中再次遇到TabHost在addTab时挂掉:android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

Android crifan 4742浏览 0评论

【问题】

之前已经遇到过一次,TabHost在addTab时挂掉:

【已解决】Android中用TabHost去addTab添加TAB结果出错:Unable to start activity ComponentInfo: android.content.res.Resources$NotFoundException: Resource ID #0×0

现在又遇到了:

mTabHost.addTab(singleTab);

时挂掉:

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

 private void generateMainTabUI(){
//	 Thread thread = new Thread(new Runnable() {
//
// @Override
// public void run() {
        ......
       mTabHost.addTab(singleTab); 
   }
// }}); 
//
//	 thread.start();
 }

(2)mHandler中正常去调用generateMainTabUI

 @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

 @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.

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
83 queries in 0.189 seconds, using 22.13MB memory