android.view.ViewRoot$CalledFromErrorThreadException在badge视图中
我正在创建一个应用程序,从服务器获取通知计数,该服务部分工作正常。在那次计数中,我被显示在徽章视图中,这也很好。当我点击徽章视图时,我发现了问题 Logcatandroid.view.ViewRoot$CalledFromErrorThreadException在badge视图中,android,notifications,badge,ui-thread,Android,Notifications,Badge,Ui Thread,我正在创建一个应用程序,从服务器获取通知计数,该服务部分工作正常。在那次计数中,我被显示在徽章视图中,这也很好。当我点击徽章视图时,我发现了问题 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. 0
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)
代码
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();
}
}
无论何时,如果您正在从其他线程进行UI更改,请尝试使用
mActivity.runOnUiThread(new Runnable() {
public void run() {
}
});
我认为您在notifiyCount.hide(true);||中进行了UI更改尝试在onCreate()方法中获取活动实例。我已经尝试过了。那时候也有同样的问题@Yuvaraja@MaanKaratePandi请参阅其他部分,您没有运行UI线程| |尝试使用mActivity.runOnUiThread()