1

我试图在没有可点击项目的 Android 2.3 中显示自定义通知。

这是我的 XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="10dp" >

    <ImageView
        android:id="@+id/image"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_alignParentLeft="true"
        android:layout_marginRight="10dp"
        android:contentDescription="@string/app_name"
        android:scaleType="fitXY"
        android:src="@drawable/icon" />

    <LinearLayout
        android:id="@+id/linearlayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@id/image"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/title"
            style="@style/NotificationTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="This is event name" />

        <TextView
            android:id="@+id/text"
            style="@style/NotificationText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="by Name Name" />
    </LinearLayout>


</RelativeLayout>

这是我的代码:

public void show(PicoEvent event)
{
    Notification notification = new Notification();
    NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
    RemoteViews contentView = new RemoteViews(getClass().getPackage().getName(), R.layout.notification);
    Intent notificationIntent = new Intent(context, MainActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);     
    contentView.setTextViewText(R.id.title, event.getName());
    contentView.setTextViewText(R.id.text, event.getPlace());       
    notification.when = 0;
    notification.tickerText = "New Event is now starting";
    notification.icon = R.drawable.icon;        
    notification.contentIntent = contentIntent;
    notification.contentView = contentView;
    notificationManager.notify(0, notification);
}

然而我总是得到异常 logcat:

09-04 12:15:26.140: E/AndroidRuntime(13350): android.app.RemoteServiceException: Bad notification posted from package com.coapps.pico: Couldn't expand RemoteViews for: StatusBarNotification(package=com.coapps.pico id=0 tag=null notification=Notification(vibrate=null,sound=null,defaults=0x0,flags=0x0))
09-04 12:15:26.140: E/AndroidRuntime(13350):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1048)
09-04 12:15:26.140: E/AndroidRuntime(13350):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-04 12:15:26.140: E/AndroidRuntime(13350):    at android.os.Looper.loop(Looper.java:130)
09-04 12:15:26.140: E/AndroidRuntime(13350):    at android.app.ActivityThread.main(ActivityThread.java:3691)
09-04 12:15:26.140: E/AndroidRuntime(13350):    at java.lang.reflect.Method.invokeNative(Native Method)
09-04 12:15:26.140: E/AndroidRuntime(13350):    at java.lang.reflect.Method.invoke(Method.java:507)
09-04 12:15:26.140: E/AndroidRuntime(13350):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
09-04 12:15:26.140: E/AndroidRuntime(13350):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
09-04 12:15:26.140: E/AndroidRuntime(13350):    at dalvik.system.NativeStart.main(Native Method)
4

0 回答 0