今天写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. CC Arithmetic Progressions (FFT + 分块处理)

    转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents    by---cxlove 题目:给出n个数,选出三个数,按下标顺序形成等差数 ...

  2. 清除IE下链接虚线框

    方法一:利用javascript的onfocus事件,实现如下: Html代码 <a href="http://www.www.baidu.com"  onfocus=&qu ...

  3. PHP生成数字+字符混合型字符串

    以下是一个用PHP随机生成字符+数字混合型的随机字符串,可用来生成会员ID.用户密码/密钥等内容,函数简单,代码如下: <?php function generate_rand($l){ $c= ...

  4. Symfony2 EventDispatcher组件

            一个插件系统中,A插件在不影响其它插件的前提下,添加新的方法,或者在一个方法运行前做一些准备工作,通过继承来实现扩展是很不容易的,由于插件之间的关联关系,A插件的改变也会使得关联的插件 ...

  5. phpEXCEL操作全解

    phpExcel中文帮助手册,列举了各种属性,以及常用的操作方法,难得是每一个都用实例加以说明,希望对大家有所帮助. phpExcel中文帮助手册,不可多得的好文章,供大家学习参考. 1.设置exce ...

  6. Bug :”解压压缩文件失败: cpio; 在头中不存在归档“

    问题描述: 在rpm包目录下执行rpm -ivh *rpm -force时,出现标题错误 解决办法: *src.rpm包也就源码包不能被直接进行安装,需要先将src.rpm包进行编译生成二进制的rpm ...

  7. python中的列表和字典

    列表和字典的区别: 列表是有序排列的一些物件,而字典是将一些物件(键)对应到另外一些物件(值)的数据结构; 应用场景: 字典 各种需要通过某个值去查看另一个值的场合,也就是一个虚拟的“查询表”,实现方 ...

  8. 不要if else的编程

    http://news.cnblogs.com/n/194216/ 英文原文:Unconditional Programming ] 本文作者介绍 Michael Feathers Michael F ...

  9. Unity3d中的Awake()、OnEnable()、Start()等默认函数的执行顺序和生命周期

    Awake()在MonoBehavior创建后就立刻调用,在脚本实例的整个生命周期中,Awake函数仅执行一次:如果游戏对象(即gameObject)的初始状态为关闭状态,那么运行程序,Awake函数 ...

  10. clone database and rename

    使用 management studio right click database -> Tasks -> Generate Scripts -> next until " ...