• Register
0 votes

Problem :

I have tried to built a simple music player in Android. My view for each song contains the SeekBar.

It works fine for me. Now I want the timer counting the seconds/minutes of a progress of a song. So I put the TextView in a layout, get it with the findViewById() in onCreate(), and put it in run() after the progress.setProgress(pos):

String mytime = String.format("%d:%d",

But the last line gives me below exception:

“android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.”

I am doing basically a same thing here as I am doing with my SeekBar - creating a view in onCreate, then just touching it in run() – and then it doesn't give me any complaint.

7 5 2
3,870 points

Please log in or register to answer this question.

1 Answer

0 votes

Solution :

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

runOnMyUiThread(new Runnable() {

    public void run() {

        // Put that updates your UI


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

9 7 4
38,600 points

Related questions

0 votes
2 answers 699 views
Problem : I am beginner to Android Programming I am trying to draw the plot/chart for the very first time. Currently I am running different kinds of sample codes and my emulator is using three different types of free libraries but nothing is apearing in the layout screen. ... (1378): Skipped 55 frames! The application may be doing too much work on its main thread. How can I fix this issue?
asked Feb 28, 2020 mphil 2.3k points
0 votes
1 answer 104 views
Problem : I am facing error on below step objUDMCountryStandards.Country = txtMySearchCountry.Text.Trim() != string.Empty ? txtMySearchCountry.Text : null; in the get grid data throws error as below : “The calling thread cannot access this object because a different thread owns it.” Is there anything wrong with my code?
asked Jan 7, 2020 alecxe 7.5k points
1 vote
1 answer 36 views
Problem : In the following step objUDMCountryStandards.Country = txtSearchCountry.Text.Trim() != string.Empty ? txtSearchCountry.Text : null; in get grid data throws the exception as below : The calling thread cannot access this object because a different thread owns it. What is wrong in my code?
asked Dec 28, 2019 alecxe 7.5k points
0 votes
1 answer 23 views
Problem I never have developed multithreading applications before, so I am a bit confused how to make one thread to wait for another. In general, I have UI thread and 2 other threads, which do some processing. I need the processing to be executed in different thread (not the UI one) in ... Task t2 = Task.Factory.StartNew(new Action(() => { //Do another stuff here })); } How can I sync t1 and t2?
asked Nov 19, 2020 alexh 2.4k points
0 votes
1 answer 84 views
Problem: I have only fundamental knowledge about Android. I am simply trying to execute the test for the android app but I am facing the below error. I want to know the cause and solution for the below android error. java ... at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
asked Aug 25, 2020 Raphael Pacheco 4.9k points
0 votes
1 answer 21 views
The objective is to enter the number of characters that the string will have, and find the letter that only repeats once accum_minor, then show its position. I did this by comparing the string with another that contains the letters of the alphabet. But when showing the position of the ... << letter_pos << "moss found";   return 0; } Input example: "xvxx". Output example: v repeats 1. x repeats 3.
asked Aug 16, 2020 game 4.6k points
0 votes
0 answers 17 views
Problem: Which of the following storage space types can only be created with three or more physical disks? Two-way mirror, striped, simple, three-way mirror, or parity
asked Jul 21, 2020 HenryL 620 points
0 votes
0 answers 5 views
Problem: you can shut down an activity by calling its ______ method.
asked 3 days ago Dan phillip 1.9k points
0 votes
1 answer 248 views
Cannot create controller in thread that did not call Looper.prepare () What does the following exception mean? How can I fix it? Here is the code: Toast toast = Toast.makeText (mContext, "Something", Toast.LENGTH_SHORT); This is the exception: Java.lang.RuntimeException: Cannot ... :121)      on Android.widget.Toast. <init> (Toast.Java:68)      on Android.widget.Toast.makeText (Toast.Java:231)
asked Aug 17, 2020 game 4.6k points