我正在创建一个应用程序,它从服务器获取通知计数,该服务部分正常工作。在那个计数我在徽章视图中显示这也很好。当我点击徽章视图的那段时间我遇到了问题
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)
代码
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();
}
}
答案 0 :(得分:1)
如果您从其他线程进行UI更改,请尝试使用
mActivity.runOnUiThread(new Runnable() {
public void run() {
}
});
我认为您在notifiyCount.hide中进行了UI更改(true); ||尝试在onCreate()方法中获取活动实例。