CalledFromWrongThreadException:只有创建了一个视图层次可以触摸的意见原来的线程 [英] CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch views

查看:439
本文介绍了CalledFromWrongThreadException:只有创建了一个视图层次可以触摸的意见原来的线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,在Android的出现以下错误:

  

CalledFromWrongThreadException ;:只   创建一个原来的线程   视图层次可以触摸自己的看法

看来,当我试图在我的活动更新一个TextView的情况发生,则调用来更新TextView的是从我的活动范围内,但我仍然得到上述错误。

我有这样的:

的onCreate() - 设置了按钮和文本视图。

onStateChange() - 一个监听器有关状态更改的通知,在此得到通知,如果改变的TextView说一些不同的文本

当我得到一个新的文本通知我试图改变TextView的像这样:

 ((TextView中)findViewById(R.id.title))的setText(一些文本)。
 

但我得到上述错误。

从Google上搜寻它,看来我应该使用一个处理程序来改变TextView的或者使用的AsyncTask?

谁能解释一下哪一个会更好用,为什么?

编辑:添加code片段:


 公共无效的onCreate(包savedInstanceState){
            super.onCreate(savedInstanceState);

            requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

            的setContentView(R.layout.my);

            。getWindow()setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.my_title);

            ((TextView中)findViewById(R.id.time))的setText(你好文字);


            findViewById(R.id.keyboardimage).setOnClickListener(新OnClickListener(){
                公共无效的onClick(视图v){

                    意图dialIntent =新的意图(Intent.ACTION_DIAL,Uri.parse(电话:));
                    startActivity(dialIntent);

                        dispatchKeyEvent(新的KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.FLAG_SOFT_KEYBOARD));
                        dispatchKeyEvent(新的KeyEvent(KeyEvent.ACTION_UP,KeyEvent.KEY code_BACK));
                }
        });

     }
 


  //回调运行服务

私人最终ICallDialogActivity.Stub iCallDialogActivity =新ICallDialogActivity.Stub(){

@覆盖
公共无效onStateChanged(INT callState)
                将抛出RemoteException {
            开关(callState){
            案例GlobalData.CALL_STATUS_IDLE:

                打破;

            案例GlobalData.CALL_STATUS_DISCONNECTING:
                byeSetup();
                打破;
    }

};
 


 公共无效byeSetup(){

            ((TextView中)findViewById(R.id.time))的setText(再见文本​​)。

            findViewById(R.id.keyboardimage).setOnClickListener(新OnClickListener(){
                公共无效的onClick(视图v){
                    //无效按钮
                }});
}
 

解决方案

看起来像你是错误的线程。尝试使用一个处理程序来更新正确的线程上的GUI。请参见处理昂贵的操作从android.com UI线程的例子。基本上你会换 byeSetup 的Runnable 并用处理程序实例。

 处理程序刷新=新的处理程序(Looper.getMainLooper());
refresh.post(新的Runnable(){
    公共无效的run()
    {
        byeSetup();
    }
});
 

I have an issue with the following error in Android:

CalledFromWrongThreadException;: Only the original thread that created a view hierarchy can touch its views

It appears to happen when I try to update a Textview in my Activity, the call to update the TextView is from within my Activity but I still get the above error.

I have it like this:

onCreate() -sets up the buttons and the text view.

onStateChange() - a listener for notifications about state changes, when this gets notification if changes the TextView to say some different text.

When I get notification of a new text I try to change the TextView as so:

((TextView)findViewById(R.id.title)).setText("Some Text");

But I get the above Error.

From googling it, it appears I should use a handler to change the TextView or maybe use AsyncTask?

Could anyone explain which one would be better to use and why?

EDIT: ADDED CODE SNIPPETS:


     public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);  

            setContentView(R.layout.my);

            getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.my_title);  

            ((TextView)findViewById(R.id.time)).setText("Hello Text");


            findViewById(R.id.keyboardimage).setOnClickListener(new OnClickListener() {
                public void onClick(View v) {

                    Intent dialIntent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"));
                    startActivity(dialIntent);

                        dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.FLAG_SOFT_KEYBOARD));
                        dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_BACK));       
                }
        });

     }


//CallBacks from running Service

private final ICallDialogActivity.Stub iCallDialogActivity = new ICallDialogActivity.Stub(){

@Override
public void onStateChanged(int callState)
                throws RemoteException {    
            switch(callState){
            case GlobalData.CALL_STATUS_IDLE:

                break;

            case GlobalData.CALL_STATUS_DISCONNECTING:
                byeSetup();
                break;
    } 

};


public void byeSetup(){

            ((TextView)findViewById(R.id.time)).setText("Bye Text");

            findViewById(R.id.keyboardimage).setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    //Void the Button
                }});
}

解决方案

Look like you are on the wrong thread. Try using a Handler to update the GUI on the right thread. See Handling Expensive Operations in the UI Thread example from android.com. Basically you would wrap byeSetup in a Runnable and invoke it with a Handler instance.

Handler refresh = new Handler(Looper.getMainLooper());
refresh.post(new Runnable() {
    public void run()
    {
        byeSetup();
    }
});

这篇关于CalledFromWrongThreadException:只有创建了一个视图层次可以触摸的意见原来的线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆