今天写notification练习时,误将NotificationManager.notify(0, notification);写成notification.notify(); 代码如下

public void notification() {
  NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
  Notification.Builder builder = new Builder(this);
  builder.setAutoCancel(true);
  builder.setContentTitle("通知")
    .setContentText("拨打电话")
    .setSmallIcon(R.drawable.ic_launcher)
    .setLargeIcon(
  BitmapFactory.decodeResource(getResources(),
  R.drawable.call));
  Notification notification = builder.getNotification();
  //nm.notify(0, notification);
  notification.notify();
}

错误日志

02-14 08:14:55.771: E/AndroidRuntime(25572): FATAL EXCEPTION: main
02-14 08:14:55.771: E/AndroidRuntime(25572): java.lang.IllegalStateException: Could not execute method of the activity
02-14 08:14:55.771: E/AndroidRuntime(25572): at android.view.View$1.onClick(View.java:3598)
02-14 08:14:55.771: E/AndroidRuntime(25572): at android.view.View.performClick(View.java:4091)
02-14 08:14:55.771: E/AndroidRuntime(25572): at android.view.View$PerformClick.run(View.java:17072)
02-14 08:14:55.771: E/AndroidRuntime(25572): at android.os.Handler.handleCallback(Handler.java:615)
02-14 08:14:55.771: E/AndroidRuntime(25572): at android.os.Handler.dispatchMessage(Handler.java:92)
02-14 08:14:55.771: E/AndroidRuntime(25572): at android.os.Looper.loop(Looper.java:153)
02-14 08:14:55.771: E/AndroidRuntime(25572): at android.app.ActivityThread.main(ActivityThread.java:5000)
02-14 08:14:55.771: E/AndroidRuntime(25572): at java.lang.reflect.Method.invokeNative(Native Method)
02-14 08:14:55.771: E/AndroidRuntime(25572): at java.lang.reflect.Method.invoke(Method.java:511)
02-14 08:14:55.771: E/AndroidRuntime(25572): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
02-14 08:14:55.771: E/AndroidRuntime(25572): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
02-14 08:14:55.771: E/AndroidRuntime(25572): at dalvik.system.NativeStart.main(Native Method)
02-14 08:14:55.771: E/AndroidRuntime(25572): Caused by: java.lang.reflect.InvocationTargetException
02-14 08:14:55.771: E/AndroidRuntime(25572): at java.lang.reflect.Method.invokeNative(Native Method)
02-14 08:14:55.771: E/AndroidRuntime(25572): at java.lang.reflect.Method.invoke(Method.java:511)
02-14 08:14:55.771: E/AndroidRuntime(25572): at android.view.View$1.onClick(View.java:3593)
02-14 08:14:55.771: E/AndroidRuntime(25572): ... 11 more
02-14 08:14:55.771: E/AndroidRuntime(25572): Caused by: java.lang.IllegalMonitorStateException: object not locked by thread before notify()
02-14 08:14:55.771: E/AndroidRuntime(25572): at java.lang.Object.notify(Native Method)
02-14 08:14:55.771: E/AndroidRuntime(25572): at com.example.notificationtest.MainActivity.notification(MainActivity.java:37)
02-14 08:14:55.771: E/AndroidRuntime(25572): ... 14 more

错误很明显:object not locked by thread before notify() , 对象在notify(唤醒)前没有被锁死,查看notify源码,进入Object类中(居然在Object中,一定是调用错了)

/**
* Causes a thread which is waiting on this object's monitor (by means of
* calling one of the {@code wait()} methods) to be woken up. If more than
* one thread is waiting, one of them is chosen at the discretion of the
* VM. The chosen thread will not run immediately. The thread
* that called {@code notify()} has to release the object's monitor first.
* Also, the chosen thread still has to compete against other threads that
* try to synchronize on the same object.
...

*/
public final native void notify();

仔细一看才发现,Object.notify()和wait是用于对对象同步操作用的方法,和notification完全不搭嘎。

我们再来看看NotificationManager.notify(0, notification)方法,进入NotificationManager中,

/**
* Post a notification to be shown in the status bar. If a notification with
* the same id has already been posted by your application and has not yet been canceled, it
* will be replaced by the updated information.
...
*/
public void notify(int id, Notification notification),从注释我们可以发现此方法用于显示通知,并且如果id相同的话,用新的notification替换原先的notification

notification:object not locked by thread before notify()的更多相关文章

  1. Object not locked by thread before notify() in onPostExecute

    Ask Question Asked 5 years, 4 months ago Active 3 years, 9 months ago Viewed 56k time 41 2 I try to ...

  2. Java Thread wait, notify and notifyAll Example

    Java Thread wait, notify and notifyAll Example Java线程中的使用的wait,notify和nitifyAll方法示例. The Object clas ...

  3. [译]Java Thread wait, notify和notifyAll示例

    Java Thread wait, notify和notifyAll示例 Java上的Object类定义了三个final方法用于不同线程间关于某资源上的锁状态交互,这三个方法是:wait(), not ...

  4. Object的wait和Thread的sleep

    Object的wait() wait()搭配notify(),nofityAll()使用. 线程获取到对象锁之后,执行wait()就会释放对象锁,同时线程挂起,直到其他线程获取到对象锁并执行notif ...

  5. 为什么等待和通知是在 Object 类而不是 Thread 中声明的?

    一个棘手的 Java 问题,如果 Java编程语言不是你设计的,你怎么能回答这个问题呢.Java编程的常识和深入了解有助于回答这种棘手的 Java 核心方面的面试问题.为什么 wait,notify ...

  6. AttributeError: 'module' object has no attribute 'Thread'

    $ python thread.py starting at: 2015-08-05 00:24:24Traceback (most recent call last):  File "th ...

  7. 'module' object has no attribute 'Thread'解决方法及模块加载顺序

    源码片段: class myThread(threading.Thread): def __init__(self, threadID, name, counter): threading.Threa ...

  8. Thread线程notify方法的自我理解

    感谢博主:http://zy19982004.iteye.com/blog/1626916 这篇博文给予我线程知识很大的帮助 知识背景:(1)wait().notify()均是Object的方法,故每 ...

  9. Thread wait notify sleep

    wait: 必须暂定当前正在执行的线程,并释放资源锁,让其他线程可以有机会运行 notify/notifyall: 唤醒因锁池中的线程,使之运行 wait与sleep区别 对于sleep()方法,我们 ...

随机推荐

  1. javascript 不用ajax 用 iframe 子域名下做到ajax post数据

    最近在一个项目中遇到了ajax跨域的问题,情况如下.有三个域名分别是 a.xx.com b.xx.com c.xx.com 这三个域名都会用用ajax post方式相互读取数据.文笔不好, 不写了妈蛋 ...

  2. tcpdump使用和TCP/IP包分析

    关于tcpdump如何抓包,本文不再总结,可以查看 tcpdump的官方地址查看http://www.tcpdump.org 本文重点记录两个部分:           第一部分:tcpdump所抓包 ...

  3. Sicily connect components in undirected graph

    题目介绍: 输入一个简单无向图,求出图中连通块的数目. Input 输入的第一行包含两个整数n和m,n是图的顶点数,m是边数.1<=n<=1000,0<=m<=10000. 以 ...

  4. 转:全志A20 GPIO 总结文档

    链接: http://blog.csdn.net/chwenj/article/details/42190745 /* * author:          chwenj@gmail.com. * A ...

  5. Ionic 测试针对Andorid平台

    环境准备: (1)nodejs环境以及配置完成: y@y:~$ node -v v0.12.2 (2)Android SDK 环境变量已经配置完毕: export ANDROID_SDK=/home/ ...

  6. SSL 证书申请(居然还可以在淘宝上购买)

    免费的目前有 2 个国内的:免费SSL证书申请国外的:StartSSL™ Certificates & Public Key Infrastructure 备注:其实,国内的这家的根证书,也是 ...

  7. Linux系统编程(33)—— socket编程之TCP程序的错误处理

    上一篇的例子不仅功能简单,而且简单到几乎没有什么错误处理,我们知道,系统调用不能保证每次都成功,必须进行出错处理,这样一方面可以保证程序逻辑正常,另一方面可以迅速得到故障信息. 为使错误处理的代码不影 ...

  8. 【转】Java多线程操作局部变量与全局变量

    原文网址:http://blog.csdn.net/undoner/article/details/12849661 在这篇文章里,我们首先阐述什么是同步,不同步有什么问题,然后讨论可以采取哪些措施控 ...

  9. MFC之按键消息(长按处理)

    想要实现长按键的一些控制,查了查可以通过捕获键盘事件,然后处理按键时需要进行的操作.下面简单的实现左右按键界面更新数值加减.  1. 重载PreTranslateMessage(MSG* pMsg)函 ...

  10. VS013的单元测试去哪里了

    需要安装这个插件 http://visualstudiogallery.msdn.microsoft.com/45208924-e7b0-45df-8cff-165b505a38d7