对象在onPostExecute中的notify()之前未被线程locking

我尝试通知在onPostExecute主要类的列表视图的适配器,但我收到错误:java.lang.IllegalMonitorStateException:通过notify()之前线程locking对象

@Override protected void onPostExecute(String result) { popularfragment.adapter.notifyDataSetChanged(); recentfragment.adapter.notifyDataSetChanged(); } 

Solutions Collecting From Web of "对象在onPostExecute中的notify()之前未被线程locking"

.notify()方法必须在synchronized上下文中调用,也就是从synchronized块中调用。

调用.notify()时抛出java.lang.IllegalMonitorStateException ,该对象不用作调用notify的同步块的锁。 例如,下面的工作;

 synchronized(obj){ obj.notify(); } 

但这会抛出exception;

 synchronized(obj){ // notify() is being called here when the thread and // synchronized block does not own the lock on the object. anotherObj.notify(); } 

参考;

  • IllegalMonitorStateException API
  • 如何使用等待和通知

我有同样的错误,但(对我来说)Rudi Kershawbuild议的答案不是问题…我以错误的方式调用了Notification的notify() (请参阅这两个片段的最后一行 ):

不工作:

 public void update() { mBuilder.setSmallIcon(R.drawable.ic_launcher) .setPriority(AesPrefs.getInt(R.string.PRIORITY_NOTIFICATION_BATTERY, NotificationCompat.PRIORITY_MAX)) .setOngoing(true); mBuilder.setWhen(AesPrefs.getLong(Loader.gStr(R.string.LAST_FIRED_BATTERY_NOTIFICATION) + Const.START_CLIPBOARD_NOTIFICATION_DELAYED, -1)); mManager.notify(); // <- lil' mistake } 

加工:

 public void update() { mBuilder.setSmallIcon(R.drawable.ic_launcher) .setPriority(AesPrefs.getInt(R.string.PRIORITY_NOTIFICATION_BATTERY, NotificationCompat.PRIORITY_MAX)) .setOngoing(true); mBuilder.setWhen(AesPrefs.getLong(Loader.gStr(R.string.LAST_FIRED_BATTERY_NOTIFICATION) + Const.START_CLIPBOARD_NOTIFICATION_DELAYED, -1)); mManager.notify(Const.NOTIFICATION_CLIPBOARD, mBuilder.build()); // <- ok ;-) }