CalledFromWrongThreadException: only the source thread that created the view hierarchy can concern views

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) { //Void the Button }}); } 
+47
android handler android-asynctask
Jul 19 '10 at 10:13
source share
6 answers

Looks like you were wrong. Try using the Handler to update the GUI in the right branch. See Handling costly operations in the user interface stream from android.com. Basically, you would byeSetup in Runnable and call it with an instance of Handler .

 Handler refresh = new Handler(Looper.getMainLooper()); refresh.post(new Runnable() { public void run() { byeSetup(); } }); 
+74
Jul 19 '10 at 12:36
source share

Extension to willcodejavaforfood answer for clarity and implementation ...

I got this to work, and the below shows how I did it. I execute several processing threads in the service, so other solutions executed in the Activity do not work, for example runOnUiThread (new Runnable () {} ...

Put this at the top of your class of service so that it is available everywhere in this class:

 Handler handler; 

Put this in your onCreate class class or something that loads into the main service thread

 handler= new Handler(Looper.getMainLooper()); 

Put this inside your extra stream in the "post back" code to get it running in the user interface or service user interface (whatevers called it):

 handler.post(new Runnable() { public void run() { playNext(); //or whatever method you want to call thats currently not working } }); 
+3
Jan 07 '15 at 3:17
source share

For others, just replace byeSetup (); with your statements or code methods. byeSetup () is a sampling method. Hope this saves you some time.

+2
May 25 '12 at 21:36
source share

when the change is associated with the main thread ( UiThread ). Use it inside another thread to change any view.

 runOnUiThread(new Runnable() { @Override public void run() { // TODO your Code et_Pass.setText(""); } }); 
+2
Nov 13 '17 at 9:15
source share

Another approach, this time using android.os.Message

Define android.os.Handler as a field inside your activity:

 private final Handler myTextHandler = new Handler(new Handler.Callback() { @Override public boolean handleMessage(Message stringMessage) { textView.append((String) stringMessage.obj); return true; } }); 

Then feed it from another thread as follows:

 Message stringMessage = Message.obtain(myTextHandler); stringMessage.obj = "Hello!"; stringMessage.sendToTarget(); 
0
Mar 27 '17 at 22:24
source share

You can use the inline post submission method to update content in another branch, as I use the edit text in kotlin.

 address_box.post { address_box.text="my text"} 
0
Nov 01 '18 at 15:56
source share



All Articles