Android "Only the Original Thread That Created a View Hierarchy Can Touch Its Views."

Android Only the original thread that created a view hierarchy can touch its views.

You have to move the portion of the background task that updates the UI onto the main thread. There is a simple piece of code for this:

runOnUiThread(new Runnable() {

@Override
public void run() {

// Stuff that updates the UI

}
});

Documentation for Activity.runOnUiThread.

Just nest this inside the method that is running in the background, and then copy paste the code that implements any updates in the middle of the block. Include only the smallest amount of code possible, otherwise you start to defeat the purpose of the background thread.

Android: only the original thread that created a view hierarchy can touch its views when calling invalidate()

Runs the specified action on the UI thread.

I would like to recommend read this site runOnUiThread

runOnUiThread(new Runnable() {
@Override
public void run() {
// call the invalidate()
}
});

Only the original thread that created a view hierarchy can touch its views?

It is because you're trying to touch the view while not in the UI thread.

Quick fix looks like this:

code before

score.setText(scorenewe.toString());

code after:

new Handler(Looper.getMainLooper()).post(new Runnable(){
@Override
public void run() {
score.setText(scorenewe.toString());
}
});

This way you will tell Android framework to run this line of code in the main UI thread, where you can touch any view you want P.S. read this

Android “Only the original thread that created a view hierarchy can touch its views.” error in for loop

That's because any view can be only modify in the main thread or ui Thread. Try running onSpeedChanged() using runOnUiThread(). Like this:

    Thread thread=new Thread(){
@Override
public void run() {
float i;
try {

for ( i = 0; i <= 100; i++) {
runOnUiThread(new Runnable() {
public void run() {
Speedometer1.onSpeedChanged(Speedometer1.getCurrentSpeed(i) + 8);
Speedometer2.onSpeedChanged(Speedometer2.getCurrentSpeed(i) + 8);
Speedometer3.onSpeedChanged(Speedometer3.getCurrentSpeed(i) + 8);
Speedometer4.onSpeedChanged(Speedometer4.getCurrentSpeed(i) + 8);
Speedometer5.onSpeedChanged(Speedometer5.getCurrentSpeed(i) + 8);
Speedometer6.onSpeedChanged(Speedometer6.getCurrentSpeed(i) + 8)

}

});


sleep(500);
}
}
catch (InterruptedException e)
{e.printStackTrace();}
}
};thread.start();

Only the original thread that created a view hierarchy can touch its views [2]

The timer thread runs on a different thread than the views

Change your code to this

val textView: TextView = findViewById(R.id.dateAndTime)

Timer().schedule(object : TimerTask() {
override fun run() {

val simpleDateFormat = SimpleDateFormat("HH:mm:ss z")
val currentDateAndTime: String = simpleDateFormat.format(Date())

//This does the trick
runOnUiThread{
textView.text = currentDateAndTime
}
}
}, 1000)

How to solve the error : Only the original thread that created a view hierarchy can touch its views

You need to put your code that updates the UI in the UI thread:

runOnUiThread(new Runnable() {
@Override
public void run() {
//your code here to update UI
}
});

How to fix Only the original thread that created a view hierarchy can touch its views in Android Fragment?

The fastest way to resolve the problem is, wrap all "tv0.setText()" call inside receive() function to another "runOnUiThread" block. Like this:

public void receive(Map<String, String> result) {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
tv0.setText(result.get("platform"));
tv1.setText(result.get("board-name"));
tv2.setText(result.get("version"));
tv3.setText(result.get("uptime"));
tv4.setText(String.format("%s %%", result.get("cpu-load")));
tv5.setText(String.format("%s MB", Integer.parseInt(result.get("free-memory")) / (1024*1024)));
tv6.setText(String.format("%s MB", Integer.parseInt(result.get("total-memory")) / (1024*1024)));
tv7.setText(String.format("%s MB", Integer.parseInt(result.get("free-hdd-space")) / (1024*1024)));
tv8.setText(String.format("%s MB", Integer.parseInt(result.get("total-hdd-space")) / (1024*1024)));
});
}

and the outter "getActivity().runOnUiThread" can be removed.



Related Topics



Leave a reply



Submit