`

CalledFromWrongThreadException: Only the original thread that created a view

阅读更多
    private void btnfinishClick(View v)
    {        
        new Thread(new MyRunnable()).start();
    }
    
    class MyRunnable implements Runnable{

        @Override
        public void run()
        {
            try
            {
                setContentView(R.layout.activity_call_finish);
                TextView tv = (TextView)findViewById(R.id.name);
                tv.setText(number);
                
                TextView prompt = (TextView)findViewById(R.id.prompt);
                prompt.setText("通话结束");
                
                TextView ct = (TextView)findViewById(R.id.call_chronometer);
                ct.setText(call_chronometer.getText());
                
                Thread.sleep(1000);
                
                myHandler.sendEmptyMessage(1);
            }
            catch (InterruptedException e)
            {
                e.printStackTrace();
            }
        }
    }


在通话结束时跳转到一个过渡界面,而上面的写法是将更新UI的代码写入到UI子线程中,所以会报错:
CalledFromWrongThreadException: Only the original thread that created a view

正确的做法时
将:
                setContentView(R.layout.activity_call_finish);
                TextView tv = (TextView)findViewById(R.id.name);
                tv.setText(number);
                
                TextView prompt = (TextView)findViewById(R.id.prompt);
                prompt.setText("通话结束");
                
                TextView ct = (TextView)findViewById(R.id.call_chronometer);
                ct.setText(call_chronometer.getText());


代码从子线程中提到btnfinishClick()里面,这样就不会报错了
分享到:
评论

相关推荐

    问题:android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original

    问题:android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a 我的场景是 LinearLayout.LayoutParams params11 = new LinearLayout.LayoutParams(264, LinearLayout....

    开新线程引发的常见3个异常

    2.android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. 3.java.lang.RuntimeException: Can't create handler inside thread ...

    Android 子线程更新UI

    android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. 这个异常为何不是 Only the main thread that created a view hierarchy ...

    Android实现在子线程中更新Activity中UI的方法

    本文实例讲述了Android实现在子线程中更新Activity中...ERROR/AndroidRuntime(1222): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its

    详解Android UI更新的几种方法

    如果是在WT进行UI的更新,则会抛出异常,android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.(只有创建这个View的原

    为什么能在子线程通过setText进行更新UI

    为什么可以在子线程通过setText进行更新UI ... Only the original thread that created a view hierarchy can touch its views.); } } 一般情况下在子线程更新UI是会报错的,因为在ViewRootImpl

    【Android】CalledFromWrongThreadException 深入源码分析

    在使用 dialog 的时候,因为线程问题,在调用 dismiss() 方法的时候,出现如下常见的 crash–Only the original thread that created a view hierarchy can touch its views.,堆栈信息如下: threadName:main,...

Global site tag (gtag.js) - Google Analytics