应用程序在尝试加载通知时崩溃

时间:2016-06-02 09:22:14

标签: android notifications crash remoteview

06-02 06:43:35.978 4227-4227/? I/art: Late-enabling -Xcheck:jni
06-02 06:43:36.088 4227-4227/meet.projectoklahoma W/System: ClassLoader referenced unknown path: /data/app/meet.projectoklahoma-1/lib/x86
06-02 06:43:36.183 4227-4227/meet.projectoklahoma I/GMPM: App measurement is starting up, version: 8487
06-02 06:43:36.183 4227-4227/meet.projectoklahoma I/GMPM: To enable debug logging run: adb shell setprop log.tag.GMPM VERBOSE
06-02 06:43:36.473 4227-4254/meet.projectoklahoma D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
06-02 06:43:36.516 4227-4254/meet.projectoklahoma D/libEGL: loaded /system/lib/egl/libEGL_emulation.so
06-02 06:43:36.517 4227-4254/meet.projectoklahoma D/libEGL: loaded /system/lib/egl/libGLESv1_CM_emulation.so
06-02 06:43:36.532 4227-4254/meet.projectoklahoma D/libEGL: loaded /system/lib/egl/libGLESv2_emulation.so
06-02 06:43:36.575 4227-4254/meet.projectoklahoma I/OpenGLRenderer: Initialized EGL, version 1.4
06-02 06:43:36.658 4227-4254/meet.projectoklahoma W/EGL_emulation: eglSurfaceAttrib not implemented
06-02 06:43:36.658 4227-4254/meet.projectoklahoma W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xed6fcd40, error=EGL_SUCCESS
06-02 06:44:56.177 4227-4254/meet.projectoklahoma W/EGL_emulation: eglSurfaceAttrib not implemented
06-02 06:44:56.177 4227-4254/meet.projectoklahoma W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xed6ff6c0, error=EGL_SUCCESS
06-02 06:44:56.585 4227-4227/meet.projectoklahoma I/Choreographer: Skipped 30 frames!  The application may be doing too much work on its main thread.
06-02 06:44:57.062 4227-4254/meet.projectoklahoma E/Surface: getSlotFromBufferLocked: unknown buffer: 0xf3dd7240
06-02 06:44:59.933 4227-4227/meet.projectoklahoma D/AndroidRuntime: Shutting down VM
06-02 06:44:59.934 4227-4227/meet.projectoklahoma E/AndroidRuntime: FATAL EXCEPTION: main
                                                                    Process: meet.projectoklahoma, PID: 4227
                                                                    android.app.RemoteServiceException: Bad notification posted from package meet.projectoklahoma: Couldn't expand RemoteViews for: StatusBarNotification(pkg=meet.projectoklahoma user=UserHandle{0} id=1 tag=null score=0 key=0|meet.projectoklahoma|1|null|10060: Notification(pri=0 contentView=meet.projectoklahoma/0x7f04001c vibrate=null sound=null defaults=0x0 flags=0x0 color=0x00000000 vis=PRIVATE))
                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1507)
                                                                        at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                        at android.os.Looper.loop(Looper.java:148)
                                                                        at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

用户登录后,应用程序应加载包含等待事件的通知,但它会破坏。上面的代码是我崩溃后得到的错误。

这是发送通知的代码:

private void sendNotification() {
    if (!LocalDataBase.getCurrentUser().getWaitingList().isEmpty())
    {
        int notificationID=1;
        setContentView(R.layout.activity_events_status_bar2);
        TextView eventName=(TextView)findViewById(R.id.incomingEventsNameText2) ;
        Event eventToDisplay=LocalDataBase.getCurrentUser().getWaitingList().get(0);
        eventName.setTag(eventToDisplay);
        Intent showEvents=new Intent(this, EventsStatusBarActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, showEvents, 0);
        RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.activity_events_status_bar2);
        contentView.setTextViewText(R.id.incomingEventsNameText2, eventToDisplay.getName());
        contentView.setOnClickPendingIntent(R.id.attendButton,pendingIntent);
        contentView.setOnClickPendingIntent(R.id.declineButton,pendingIntent);
        NotificationCompat.Builder eventsNotification= new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.notification_template_icon_bg)
                .setContentText("Incoming notifications")
                .setContent(contentView);
        eventsNotification.setContentIntent(pendingIntent);
        NotificationManager notificationManager=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(notificationID,eventsNotification.build());
    }


}

这是activity_events_status_bar2的代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:weightSum="1">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Event Name"
        android:tag="event"
        android:id="@+id/incomingEventsNameText2"
        android:layout_gravity="left"
        android:textColor="@android:color/black"
        android:textSize="22sp"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignBottom="@+id/attendButton" />

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Going"
        android:id="@+id/attendButton"
        android:layout_alignParentTop="true"
        android:layout_toLeftOf="@+id/declineButton"
        android:layout_toStartOf="@+id/declineButton"
        android:onClick="selectGoing"/>

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Not Going"
        android:id="@+id/declineButton"
        android:layout_alignBottom="@+id/attendButton"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:onClick="selectNotGoing"/>


</RelativeLayout>

2 个答案:

答案 0 :(得分:0)

您可能需要直接应用contentView来解决这个问题:

     NotificationCompat.Builder eventsNotification= new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.notification_template_icon_bg)
                    .setContentText("Incoming notifications")
                    .setContent(contentView);
            eventsNotification.setContentIntent(pendingIntent);
 NotificationManager notificationManager=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

     Notification myNotification = eventsNotification.build();
     notification.contentView = contentView;
     notificationManager.notify(notificationID,myNotification);

答案 1 :(得分:0)

查看这个密切相关的问题here,我认为您可以尝试获取Notification对象,然后直接设置其contentView。 以下是您需要对代码进行的一些更改:

//... the rest of your code up to this point (unchanged)
NotificationCompat.Builder eventsNotification= new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.notification_template_icon_bg)
                .setContentText("Incoming notifications")

     eventsNotification.setContentIntent(pendingIntent);
     NotificationManager notificationManager=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
     //get handle to your notification object
     Notification notification = eventsNotification.build();
     //then set contentView directly like this:
     notification.contentView = contentView;
     //finally, pass the notification over
     notificationManager.notify(notificationID,notification);

我希望这也有助于解决您的问题。