• Register
0 votes
48 views

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",
            TimeUnit.MILLISECONDS.toMinutes(pos),
            TimeUnit.MILLISECONDS.toSeconds(pos),
            TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(
                    pos))
            );
currentTime.setText(time);  

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

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

    @Override
    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 346 views
346 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 mphil 2.3k points
0 votes
1 answer 79 views
79 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 alecxe 7.5k points
1 vote
1 answer 32 views
32 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 14 views
14 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 Raphael Pacheco 4.9k points