1

. 我看到了类似的问题,但无法解决我的问题。我使用带有自定义布局的通知。在我的设备(sdk 16)上一切正常,但在 android 2.3.7 上,我的应用程序抛出错误。这是我的布局。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:layout_gravity="center_vertical"
              android:orientation="horizontal">
    <ImageView
            android:id="@+id/notifAlbum"
            android:maxWidth="80dip"
            android:adjustViewBounds="false"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:scaleType="fitCenter"/>
    <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:orientation="vertical" android:paddingLeft="10dp">
        <TextView
                android:id="@+id/notifTitle"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:singleLine="true"
                android:ellipsize="marquee" android:textSize="18sp"
                android:textColor="@color/almost_white"/>
        <TextView
                android:id="@+id/notifArtist"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:singleLine="true"
                android:ellipsize="marquee" android:textSize="14sp"/>
    </LinearLayout>
    <ImageButton
            android:id="@+id/notifPrevious"
            android:paddingTop="10dip"
            android:paddingBottom="10dip"
            android:layout_height="42dip"
            android:layout_width="wrap_content"
            android:layout_gravity="center"
            android:background="?android:attr/selectableItemBackground"
            android:scaleType="fitCenter"
            android:src="@drawable/widget_previous" android:baselineAlignBottom="false"/>
    <ImageButton
            android:id="@+id/notifPlay"
            android:paddingTop="6dip"
            android:paddingBottom="6dip"
            android:layout_height="42dip"
            android:layout_width="wrap_content"
            android:layout_gravity="center"
            android:background="?android:attr/selectableItemBackground"
            android:scaleType="fitCenter"
            android:src="@drawable/widget_play" android:paddingLeft="6dp" android:paddingRight="6dp"/>
    <ImageButton
            android:id="@+id/notifNext"
            android:paddingTop="10dip"
            android:paddingBottom="10dip"
            android:layout_height="42dip"
            android:layout_width="wrap_content"
            android:layout_gravity="center"
            android:background="?android:attr/selectableItemBackground"
            android:scaleType="fitCenter"
            android:src="@drawable/widget_next" android:longClickable="false"/>
    <ImageButton
            android:id="@+id/notifClose"
            android:layout_height="42dip"
            android:layout_width="wrap_content"
            android:layout_gravity="center"
            android:background="?android:attr/selectableItemBackground"
            android:scaleType="fitCenter"
            android:src="@drawable/close" android:paddingRight="4dp" android:paddingLeft="2dp"
            android:paddingTop="12dp" android:paddingBottom="12dp"/>
</LinearLayout>

这是错误。

android.app.RemoteServiceException: Bad notification posted from package com.perm.DoomPlay: Couldn't expand RemoteViews for: StatusBarNotification(package=com.perm.DoomPlay id=931 tag=null notification=Notification(vibrate=null,sound=null,defaults=0x0,flags=0x62))
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1056)
at android.os.Handler.dispatchMessage(Handler.java:99)
....

这是我建立通知的方式

private RemoteViews getNotifViews(int layoutId)
    {
        RemoteViews views = new RemoteViews(getPackageName(), layoutId);

        Audio audio = audios.get(indexCurrentTrack);

        views.setTextViewText(R.id.notifTitle, audio.getTitle());
        views.setTextViewText(R.id.notifArtist, audio.getArtist());

        Bitmap cover = AlbumArtGetter.getBitmapById(audio.getAid(),this);
        if (cover == null)
        {
            views.setImageViewBitmap(R.id.notifAlbum, BitmapFactory.decodeResource(getResources(), R.drawable.fallback_cover));
        }
        else
        {
            views.setImageViewBitmap(R.id.notifAlbum, cover);
        }


        views.setImageViewResource(R.id.notifPlay, isPlaying ? R.drawable.widget_pause : R.drawable.widget_play);

        ComponentName componentName = new ComponentName(this,PlayingService.class);

        Intent intentPlay = new Intent(actionPlay);
        intentPlay.setComponent(componentName);
        views.setOnClickPendingIntent(R.id.notifPlay, PendingIntent.getService(this, 0, intentPlay, 0));

        Intent intentNext = new Intent(actionNext);
        intentNext.setComponent(componentName);
        views.setOnClickPendingIntent(R.id.notifNext, PendingIntent.getService(this, 0, intentNext, 0));

        Intent intentPrevious = new Intent(actionPrevious);
        intentPrevious.setComponent(componentName);
        views.setOnClickPendingIntent(R.id.notifPrevious, PendingIntent.getService(this, 0, intentPrevious, 0));

        Intent intentClose = new Intent(actionClose);
        intentClose.setComponent(componentName);
        views.setOnClickPendingIntent(R.id.notifClose, PendingIntent.getService(this, 0, intentClose, 0));

        return views;
    }

    private Notification createNotification()
    {

        Intent intentActivity;


        intentActivity = new Intent(FullPlaybackActivity.actionReturnFull);
        intentActivity.setClass(this,FullPlaybackActivity.class);
        intentActivity.putExtra(FileSystemActivity.keyMusic,audios);
        intentActivity.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        Notification notification = new Notification();
        notification.contentView = getNotifViews(R.layout.notif);
        notification.flags |= Notification.FLAG_FOREGROUND_SERVICE;
        notification.contentIntent = PendingIntent.getActivity(this,0,intentActivity,PendingIntent.FLAG_UPDATE_CURRENT);
        notification.icon =  isPlaying ?  R.drawable.status_icon_pause : R.drawable.status_icon_play;

        return notification;
    }
4

2 回答 2

3

我有同样的错误,但这是由于

android:background="?android:attr/selectableItemBackground"

删除这条线解决了这个问题。

于 2016-01-18T21:52:30.633 回答
0

我找到了异常的原因。这是因为我使用了ImageButton在旧设备上不支持的自定义布局( api level < 11 )。可能会对某人有所帮助。

于 2013-10-14T17:12:16.487 回答