notification:object not locked by thread before notify()
今天写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()的更多相关文章
- 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 ...
- Java Thread wait, notify and notifyAll Example
Java Thread wait, notify and notifyAll Example Java线程中的使用的wait,notify和nitifyAll方法示例. The Object clas ...
- [译]Java Thread wait, notify和notifyAll示例
Java Thread wait, notify和notifyAll示例 Java上的Object类定义了三个final方法用于不同线程间关于某资源上的锁状态交互,这三个方法是:wait(), not ...
- Object的wait和Thread的sleep
Object的wait() wait()搭配notify(),nofityAll()使用. 线程获取到对象锁之后,执行wait()就会释放对象锁,同时线程挂起,直到其他线程获取到对象锁并执行notif ...
- 为什么等待和通知是在 Object 类而不是 Thread 中声明的?
一个棘手的 Java 问题,如果 Java编程语言不是你设计的,你怎么能回答这个问题呢.Java编程的常识和深入了解有助于回答这种棘手的 Java 核心方面的面试问题.为什么 wait,notify ...
- AttributeError: 'module' object has no attribute 'Thread'
$ python thread.py starting at: 2015-08-05 00:24:24Traceback (most recent call last): File "th ...
- 'module' object has no attribute 'Thread'解决方法及模块加载顺序
源码片段: class myThread(threading.Thread): def __init__(self, threadID, name, counter): threading.Threa ...
- Thread线程notify方法的自我理解
感谢博主:http://zy19982004.iteye.com/blog/1626916 这篇博文给予我线程知识很大的帮助 知识背景:(1)wait().notify()均是Object的方法,故每 ...
- Thread wait notify sleep
wait: 必须暂定当前正在执行的线程,并释放资源锁,让其他线程可以有机会运行 notify/notifyall: 唤醒因锁池中的线程,使之运行 wait与sleep区别 对于sleep()方法,我们 ...
随机推荐
- [转] linux 下 进程和线程的区别
1.进程与线程 进程是程序执行时的一个实例,即它是程序已经执行到课中程度的数据结构的汇集.从内核的观点看,进程的目的就是担当分配系统资源(CPU时间.内存等)的基本单位. 线程是进程的一个执行流,是C ...
- 《Linux内核分析》 week2作业-时间片轮转
一.基于时间片轮转调度代码的解读 代码结构主要由三个文件组成: 1.mypcb.h 2.myinterrupt.c 3.mymain.c 1.进程控制块(mypcb.h) /* CPU-specifi ...
- js本地存储解决方案(localStorage与userData)
WEB应用的快速发展,是的本地存储一些数据也成为一种重要的需求,实现的方案也有很多,最普通的就是cookie了,大家也经常都用,但是cookie的缺点是显而易见的,其他的方案比如:IE6以上的user ...
- template of class
class template will call the constructor of its member object before constructor itself......
- 高性能的EMI滤波器及其小型化设计技术
1 EMI滤波器的常见问题及发展趋势首先介绍了影响EMI滤波器性能/体积的因素及EMI滤波器的常见问题:低频传导发射高.高频传导/辐射发射高.体积大,从而分析出EMI滤波器的发展趋势为高性能和小体积, ...
- oschina企业应用
企业应用 6企业搜索引擎 20ESB企业服务总线 34LaTeX排版系统 32软电话交换机/VOIP/PBX 9邮件列表管理 42大数据 21开源医疗项目 12人力资源管理 15家庭自动化系统 16E ...
- HDU_2028——求多个数的最小公倍数
Problem Description 求n个数的最小公倍数. Input 输入包含多个测试实例,每个测试实例的开始是一个正整数n,然后是n个正整数. Output 为每组测试数据输出它们的最 ...
- 移动web app开发框架
文章地址:http://www.cnblogs.com/soulaz/p/5586787.html jQuery Mobile jQuery Mobile框架能够帮助你快速开发出支持多种移动设备的Mo ...
- HDU4432 Sum of Divisors
涉及知识点: 1. 进制转换. 2. 找因子时注意可以降低复杂度. Sum of divisors Time Limit: 2000/1000 MS (Java/Others) Memory L ...
- Makefile如何通过宏开关进行条件编译
在开发中经常会遇到需要条件编译一段代码,即: #ifdef DEBUG { 如果定义了DUBUG,则执行此段代码!} #else {否则执行此段代码!} 这就需要通过宏开关来进行条件编译,也就是常说的 ...