Question

I am create an app which is get the notify count from server, that service part working fine. In that count i was showed in badge view this also fine. when i click the badgeview that time i have the issue followed

Logcat

04-09 10:46:11.741: W/System.err(321): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
04-09 10:46:11.751: W/System.err(321):  at android.view.ViewRoot.checkThread(ViewRoot.java:2932)
04-09 10:46:11.760: W/System.err(321):  at android.view.ViewRoot.requestLayout(ViewRoot.java:629)
04-09 10:46:11.760: W/System.err(321):  at android.view.View.requestLayout(View.java:8267)
04-09 10:46:11.861: W/System.err(321):  at android.view.View.requestLayout(View.java:8267)
04-09 10:46:11.861: W/System.err(321):  at android.view.View.requestLayout(View.java:8267)
04-09 10:46:11.861: W/System.err(321):  at android.view.View.requestLayout(View.java:8267)
04-09 10:46:11.880: W/System.err(321):  at android.view.View.requestLayout(View.java:8267)
04-09 10:46:11.880: W/System.err(321):  at android.view.View.requestLayout(View.java:8267)
04-09 10:46:11.930: W/System.err(321):  at android.view.View.requestLayout(View.java:8267)
04-09 10:46:11.930: W/System.err(321):  at android.view.View.requestLayout(View.java:8267)
04-09 10:46:11.941: W/System.err(321):  at android.view.View.requestLayout(View.java:8267)
04-09 10:46:11.941: W/System.err(321):  at android.view.View.requestLayout(View.java:8267)
04-09 10:46:11.941: W/System.err(321):  at android.view.View.requestLayout(View.java:8267)
04-09 10:46:11.981: W/System.err(321):  at android.view.View.requestLayout(View.java:8267)
04-09 10:46:11.981: W/System.err(321):  at android.view.View.setFlags(View.java:4641)
04-09 10:46:11.981: W/System.err(321):  at android.view.View.setVisibility(View.java:3116)
04-09 10:46:11.981: W/System.err(321):  at com.appname.BadgeView.hide(BadgeView.java:278)
04-09 10:46:11.981: W/System.err(321):  at com.appname.BadgeView.hide(BadgeView.java:221)
04-09 10:46:11.981: W/System.err(321):  at com.appname.MainActivity$9.run(MainActivity.java:253)
04-09 10:46:11.981: W/System.err(321):  at java.lang.Thread.run(Thread.java:1019)

Code

new Thread(new Runnable() {
                    @Override
                    public void run() {

                        if (SharedUName != null) {
                            while (true) {
                                if (sharedPreferences.getInt(
                                        KP.Notification.GET_NOTIFY_OLDCOUNT, 0) != 0) {
                                    try {
                                        Thread.sleep(2000);
                                        int check = 0;
                                        final int ncount = sharedPreferences
                                                .getInt(KP.Notification.GET_NOTIFY_COUNT,
                                                        0);
                                        if (ncount != check) {
                                            runOnUiThread(new Runnable() {
                                                public void run() {// access
                                                    displayCount(ncount);
                                                }
                                            });
                                        } else {
                                            notifiyCount.hide(true);
                                        }
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                    }

                                }
                            }
                        }
                    }
                }).start();


private void displayCount(int count) {
        try {
            if (SharedUName != null) {
                if (!check == true) {
                    notifiyCount.hide(true);
                }
                notifiyCount = new BadgeView(this, imgNotification);
                notifiyCount.setText(String.valueOf(count));
                notifiyCount.setBadgePosition(BadgeView.FOCUS_LEFT);
                notifiyCount.toggle();
                check = false;
            } else {
                notifiyCount.hide();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
Was it helpful?

Solution

Where ever you are making UI changes from other thread try to use

mActivity.runOnUiThread(new Runnable() {
   public void run() {

    }
    });

I think you made UI changes in notifiyCount.hide(true); || Try to get the Activity Instance in onCreate() method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top