I have a problem with the following error in Android:
CalledFromWrongThreadException ;: Only the source thread that created the view hierarchy can relate to its views
It seems that when I try to update the Textview in my activity, the call to update the TextView comes from my activity, but I still get the above error.
I have it like this:
OnCreate () - sets buttons and text view.
onStateChange () - a state change notification listener when it receives a notification if TextView Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text -
When I receive notification of new text, I try to change the TextView like this:
((TextView)findViewById(R.id.title)).setText("Some Text");
But I get the above error.
From a googling search, it seems I should use a handler to modify the TextView, or perhaps use AsyncTask?
Can someone explain which one is better to use and why?
EDIT: CONNECTED CONTENT CODES:
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) {
android handler android-asynctask
Donal Rafferty Jul 19 '10 at 10:13 2010-07-19 10:13
source share