while using volley library ,if i want to update the listview adapter inside Response listener , should it be done using runOnUiThread? or is it already in UiThread?
Asked
Active
Viewed 1,328 times
2
-
i have tried both with and without runOnUiThread , but i am not sure if its needed or not . – Siddarth G May 12 '16 at 19:43
-
What? if it works without it, then you don't need it. I don't follow you – Tim May 12 '16 at 19:46
-
sometimes i get this exception android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. but sometimes it works fine. thats why i asked this question – Siddarth G Jun 26 '16 at 10:26
-
Why downvoting? That is a totally legit question: volley says it runs its callbacks in UI thread but without explicit switch to it you get an exception. – Ivan Apr 04 '17 at 17:18
2 Answers
1
You may get the following exception when the adapter tries to modify view objects:
android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
So, just do the following to be on the safe side:
geyActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
adapter.notifyDataSetChanged();
}
});

yrazlik
- 10,411
- 33
- 99
- 165
-
Yes this is why i had this question, and i use runOnUiThread() as you suggested.. but onResponse takes the control back to UI thread , then why does this exception arise, and why is it we have to use runOnUiThread even when we are on UiThread? – Siddarth G Jun 26 '16 at 10:29
0
call handler from volley response
@Override
public void onResponse(String response) {
Log.d("Response is: ", response);
rui = response; // i put response to public String variable rui
myHandler.post(updateRunnable);
parseJSON(response);
}
and in onCreate Activity
myHandler = new Handler();
updateRunnable = new Runnable() {
public void run() {
//call the activity method that updates the UI
updateUI();
}
};
and your update on updateUI
public void updateUI(){
t.setText(rui);
}
and make sure your id of widget is really existed.
<TextView
android:id="@+id/konten"
android:layout_width="wrap_content"
android:layout_height="wrap_content"

Fajar Rukmo
- 78
- 7
-
1`onResponse` runs on the `UIThread` so it is not necessary to use a handler. – Enzokie Dec 02 '17 at 13:05
-
my bad.. yes it works directly. so in my case the problem is because id is not well defined so it's always throw nullPoint exception. – Fajar Rukmo Dec 02 '17 at 18:46