我正在尝试制作自定义通知提醒,但代码中出现此异常后会出现崩溃。
10-11 11:13:01.445 2816 4383 E JavaBinder: *** Uncaught remote exception! (Exceptions are not yet supported across processes.)
10-11 11:13:01.445 2816 4383 E JavaBinder: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
10-11 11:13:01.445 2816 4383 E JavaBinder: at android.os.Handler.<init>(Handler.java:197)
10-11 11:13:01.445 2816 4383 E JavaBinder: at android.os.Handler.<init>(Handler.java:111)
10-11 11:13:01.445 2816 4383 E JavaBinder: at com.android.server.NotificationManagerService$9.<init>(NotificationManagerService.java:2800)
10-11 11:13:01.445 2816 4383 E JavaBinder: at com.android.server.NotificationManagerService.sendMsg(NotificationManagerService.java:2800)
10-11 11:13:01.445 2816 4383 E JavaBinder: at com.android.server.NotificationManagerService.updateSoundLocked(NotificationManagerService.java:2787)
10-11 11:13:01.445 2816 4383 E JavaBinder: at com.android.server.NotificationManagerService.enqueueNotificationInternal(NotificationManagerService.java:2055)
10-11 11:13:01.445 2816 4383 E JavaBinder: at com.android.server.NotificationManagerService.enqueueNotificationWithTag(NotificationManagerService.java:1911)
10-11 11:13:01.445 2816 4383 E JavaBinder: at android.app.INotificationManager$Stub.onTransact(INotificationManager.java:103)
10-11 11:13:01.445 2816 4383 E JavaBinder: at android.os.Binder.execTransact(Binder.java:388)
10-11 11:13:01.445 2816 4383 E JavaBinder: at dalvik.system.NativeStart.run(Native Method)
10-11 11:13:01.445 8979 8979 D AndroidRuntime: Shutting down VM
10-11 11:13:01.445 8979 8979 W dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x41786700)
10-11 11:13:01.445 2357 2631 E AudioResampler: Unsupported sample format, 1 bits, 2 channels
10-11 11:13:01.445 8979 8979 E AndroidRuntime: FATAL EXCEPTION: main
10-11 11:13:01.445 8979 8979 E AndroidRuntime: java.lang.RuntimeException: bad array lengths
10-11 11:13:01.445 8979 8979 E AndroidRuntime: at android.os.Parcel.readIntArray(Parcel.java:789)
10-11 11:13:01.445 8979 8979 E AndroidRuntime: at android.app.INotificationManager$Stub$Proxy.enqueueNotificationWithTag(INotificationManager.java:339)
10-11 11:13:01.445 8979 8979 E AndroidRuntime: at android.app.NotificationManager.notify(NotificationManager.java:139)
10-11 11:13:01.445 8979 8979 E AndroidRuntime: at android.app.NotificationManager.notify(NotificationManager.java:112)
10-11 11:13:01.445 8979 8979 E AndroidRuntime: at com.android.systemui.screenshot.SaveImageInBackgroundTask.onPostExecute(GlobalScreenshot.java:380)
10-11 11:13:01.445 8979 8979 E AndroidRuntime: at com.android.systemui.screenshot.SaveImageInBackgroundTask.onPostExecute(GlobalScreenshot.java:126)
10-11 11:13:01.445 8979 8979 E AndroidRuntime: at android.os.AsyncTask.finish(AsyncTask.java:631)
10-11 11:13:01.445 8979 8979 E AndroidRuntime: at android.os.AsyncTask.access$600(AsyncTask.java:177)
10-11 11:13:01.445 8979 8979 E AndroidRuntime: at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
10-11 11:13:01.445 8979 8979 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99)
10-11 11:13:01.445 8979 8979 E AndroidRuntime: at android.os.Looper.loop(Looper.java:137)
10-11 11:13:01.445 8979 8979 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5419)
10-11 11:13:01.445 8979 8979 E AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
10-11 11:13:01.445 8979 8979 E AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:525)
10-11 11:13:01.445 8979 8979 E AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
10-11 11:13:01.445 8979 8979 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
10-11 11:13:01.445 8979 8979 E AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
我遇到问题的代码片段是:
private void sendMsg(boolean delayed) {
if(mNotificationSoundHandler == null) {
mNotificationSoundHandler = new Handler() { //may be some prob with this
public void handleMessage(Message msg) {
super.handleMessage(msg);
Log.d(TAG, " !!!Handle Message !!!");
updateSoundLocked();
}
};
}
答案 0 :(得分:0)
您无法在UI线程外更新UI。你应该这样做:
getActivity().runOnUiThread(new Runnable() {
updateSoundLocked();
});
甚至:
private void sendMsg(boolean delayed) {
if(mNotificationSoundHandler == null) {
mNotificationSoundHandler = new Handler(Looper.getMainLooper()) {
public void handleMessage(Message msg) {
super.handleMessage(msg);
Log.d(TAG, " !!!Handle Message !!!");
updateSoundLocked();
}
};
}
}
请参阅official documentation了解更多详情。