CalledFromWrongThreadException:只有创建视图层次结构的原始线程才能触摸视图 [英] CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch views
问题描述
我在 Android 中遇到以下错误:
I have an issue with the following error in Android:
CalledFromWrongThreadException;:仅创建一个的原始线程视图层次结构可以触摸它的视图
CalledFromWrongThreadException;: Only the original thread that created a view hierarchy can touch its views
当我尝试更新我的 Activity 中的 TextView 时,似乎发生了这种情况,更新 TextView 的调用来自我的 Activity,但我仍然收到上述错误.
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.
我是这样的:
onCreate()- 设置按钮和文本视图.
onCreate() -sets up the buttons and the text view.
onStateChange() - 状态更改通知的侦听器,如果将 TextView 更改为某些不同的文本,则会收到通知.
onStateChange() - a listener for notifications about state changes, when this gets notification if changes the TextView to say some different text.
当我收到新文本的通知时,我尝试像这样更改 TextView:
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.
从谷歌搜索来看,我似乎应该使用处理程序来更改 TextView 或者使用 AsyncTask?
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?
添加代码片段:
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
}});
}
推荐答案
看起来您走错了话题.尝试使用处理程序在正确的线程上更新 GUI.请参阅 android.com 上的在 UI 线程中处理昂贵的操作 示例.基本上,您可以将 byeSetup
包装在 Runnable
中,并使用 Handler
实例调用它.
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屋!