Question

I am trying to start an animation if a particular count from DB is more than 0 and try to set the count value to a textView. I have a timer to check if the table count is more than 0 for every one second

timeMethod is called in Oncreate

private void timerMethod()
{
    //Set the schedule function and rate
    t.scheduleAtFixedRate(new TimerTask() 
    {
        @Override
        public void run() 
        {
            updatescreen();
        }

    }, 0, 1000);
}

private void updatescreen() 
{
    DB = new Databasehandler(this);
    int counter = DB.getCount();

    if (counter > 0)
    {
        Log.e("DB","Counter" +counter);

        UnreadCount = DB.getCount();
        count.setText(""+UnreadCount);

        if(AnimStarted.equalsIgnoreCase("YES"))
        { 
            image.startAnimation(anim);
            AnimStarted = "NO";
        }
    }

But by doing this I have the following error:

05-06 11:32:46.663: E/AndroidRuntime(24700): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
05-06 11:32:46.663: E/AndroidRuntime(24700):    at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6094)
05-06 11:32:46.663: E/AndroidRuntime(24700):    at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:824)
05-06 11:32:46.663: E/AndroidRuntime(24700):    at android.view.View.requestLayout(View.java:16431)
05-06 11:32:46.663: E/AndroidRuntime(24700):    at android.view.View.requestLayout(View.java:16431)
05-06 11:32:46.663: E/AndroidRuntime(24700):    at android.view.View.requestLayout(View.java:16431)
05-06 11:32:46.663: E/AndroidRuntime(24700):    at android.view.View.requestLayout(View.java:16431)
05-06 11:32:46.663: E/AndroidRuntime(24700):    at android.view.View.requestLayout(View.java:16431)
05-06 11:32:46.663: E/AndroidRuntime(24700):    at android.view.View.requestLayout(View.java:16431)
05-06 11:32:46.663: E/AndroidRuntime(24700):    at android.view.View.requestLayout(View.java:16431)
05-06 11:32:46.663: E/AndroidRuntime(24700):    at android.widget.TextView.checkForRelayout(TextView.java:6600)
05-06 11:32:46.663: E/AndroidRuntime(24700):    at android.widget.TextView.setText(TextView.java:3813)
05-06 11:32:46.663: E/AndroidRuntime(24700):    at android.widget.TextView.setText(TextView.java:3671)
05-06 11:32:46.663: E/AndroidRuntime(24700):    at android.widget.TextView.setText(TextView.java:3646)
05-06 11:32:46.663: E/AndroidRuntime(24700):    at com.jay.te.MainScreen.updatescreen(MainScreen.java:2970)
05-06 11:32:46.663: E/AndroidRuntime(24700):    at com.jay.te.MainScreen.access$31(MainScreen.java:2960)
05-06 11:32:46.663: E/AndroidRuntime(24700):    at com.jay.te.MainScreen$10.run(MainScreen.java:2954)
05-06 11:32:46.663: E/AndroidRuntime(24700):    at java.util.Timer$TimerImpl.run(Timer.java:284)

Not sure what is exact issue here? Can somebody help me fix this?

Thanks!

Was it helpful?

Solution

You cannot update UI from a non ui thread. You have a timer which runs on a background thread and you update UI in updatescreen.

Use runOnUiThread and update ui or use a Handler with a delay

  runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                           // update ui here
                        }
                 });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top