Calledfromwrongthreadexception: Only the Original Thread That Created a View Hierarchy Can Touch 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.

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 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 [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)

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();

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

You have this error because you manipulate views in another thread than the UI thread.
Your setText method modify the text of 3 TextViews (dice1, dice2, dice3).

You should call this method with runOnUiThread like this:

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button generate = (Button) findViewById(R.id.gen);
final TextView dice1 = (TextView) findViewById(R.id.dice1);
final TextView dice2 = (TextView) findViewById(R.id.dice2);
final TextView dice3 = (TextView) findViewById(R.id.dice3);

generate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
dice1.setText(" ");
dice2.setText(" ");
dice3.setText(" ");
Thread thread = new Thread()
{
@Override
public void run() {
try {
while(true) {
sleep(2000);
runOnUiThread(new Runnable() {
@Override
public void run() {
setText("lol", "lol", "lol");
}
});
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
thread.start();
}

});
}
public void setText(String d1, String d2, String d3){
dice1.setText(d1);
dice2.setText(d2);
dice3.setText(d3);
}

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

Change your startingUp() to this.

 private void startingUp() {
Thread timer = new Thread() { //new thread
public void run() {
Boolean b = true;
try {
do {
counter++;
title();
sleep(1000);

runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub

title.clearComposingText();
}
});

}
while (b == true);
} catch (IntruptedException e) {
e.printStackTrace();
}
finally {
}
};
};
timer.start();
}

You can't modify views from non-UI thread.

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.

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
}
});

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

You need to manipulate UI from main thread so in order to do that you need to tell rxandroid to notify changes on main thread so use

button.setOnClickListener(view -> restApiFactory.testService().getListTest(7)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
// ^^^^^^^^^^
.subscribe(new Observer<List<Test>>() {

and to obtain this, you need to have a dependency as

compile 'io.reactivex.rxjava2:rxandroid:2.0.1' 

and your current dependencies are used to make retrofit retuns rxAndroid type response.



Related Topics



Leave a reply



Submit