今天写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. Flume 1.4.0 User Guide

    Apache Flume is a distributed, reliable, and available system for efficiently collecting, aggregatin ...

  2. Rational Rose 7.0的使用(转)

    1.Rose如何隐藏类的属性和操作? 右击类图,选择Options->Suppress Attributes/Suppress Operations 2.Rose中如何表示双向关联? 右击关联线 ...

  3. $this->success('注册成功!');

    使用在控制器中,不是使用再模板中的

  4. DIRECTORY_SEPARATOR

    定义 php的内置变量DIRECTORY_SEPARATOR是一个显示系统分隔符的命令,DIRECTORY_SEPARATOR是php的内部常量,不需要任何定义与包含即可直接使用. 2说明   路径分 ...

  5. PHPCMS v9构建模块 - 实例之企业服务模块

    下面开始第一个实例,企业服务模块,这是个比较简单的模块,做一个抛砖的作用.   模块功能分析:企业服务,企业填写招聘申请表,管理审核之后,展示作为招聘通知的功能.   ■1.文件分布 modules文 ...

  6. 清除IE下链接虚线框

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

  7. i++ 与 ++i 的从字节码层面看二者的区别

    /** * javap命令可以对class反汇编得到其字节码文件(此命令并不是jdk8开始的,只不过jdk8中对工具进行加强,增加了一些参数,可通过 javap -help了解) * * 注意: * ...

  8. Nmap Snote

    Title:Nmap Snote --2011-11-15 21:28 用Nmap上瘾了,怕以后忘记,也就记一下. Nmap -v -sS -n -p1-65535 IP Nmap -v -sS -p ...

  9. MySQL执行外部sql脚本

    1:-/mysql_test/test.sql create table student( sno int not null primary key auto_increment, sname ) n ...

  10. iOS 9之分屏多任务(Split View)

    金田(github 示例源码) 多任务(multitasking)算是iOS9中最引人瞩目的核心新特性了,之前越狱版用户就用过类似的插件,微软的 苏菲 (Windows Surface)系列也有分屏多 ...