簡體   English   中英

使用矢量drawable時,通知會引發錯誤

[英]Notification throws error when using vector drawables

當我使用向量drawable設置通知的小圖標時,我得到以下異常:

android.app.RemoteServiceException:從包com.qbes.xxx發布的錯誤通知:無法創建圖標:StatusBarIcon(pkg = com.qbes.xxxuser = 0 id = 0x7f020082 level = 0 visible = true num = 0)

這是我的代碼:

mNotificationBuilder = new android.support.v4.app.NotificationCompat.Builder(this)
                .setDefaults(android.support.v4.app.NotificationCompat.DEFAULT_LIGHTS)
                .setSound(null)
                .setSmallIcon(R.drawable.logo_white)
                .setColor(getResources().getColor(R.color.colorPrimary))
                .setCategory(android.support.v4.app.NotificationCompat.CATEGORY_PROGRESS)
                .setContentTitle("Trip in Progress...")
                .setAutoCancel(false)
                .setProgress(0, 0, progress)
                .setOngoing(true)
                .setPriority(android.support.v4.app.NotificationCompat.PRIORITY_MAX)
                .setOnlyAlertOnce(true)
                .setContentIntent(pendingIntent);

mNotificationBuilder.setContentText(body);

mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification note = mNotificationBuilder.build();

mNotificationManager.notify(Constants.NOTIFICATION_ID_Dash, note);

和我的build.gradle (只有相關部分):

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    defaultConfig {
        applicationId "com.qbes.xxx"
        minSdkVersion 16
        targetSdkVersion 22
        versionCode 720
        versionName "0.7.20"
        vectorDrawables.useSupportLibrary = true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.2.1'
    compile 'com.android.support:design:23.2.1'
}

PS:當我使用png或jpg圖像繪制時,代碼工作正常,但在使用矢量drawable時斷開。

我一直在尋找一整天但找不到任何對我有用的東西。 有任何想法嗎。

您正在使用矢量drawable支持包。 這很好,但只適用於您的應用。 該平台不知道如何在API Level 21之前使用矢量drawable,而對於Notification ,平台是渲染資源的平台。

您可以將矢量繪制為自己渲染到由Bitmap支持的Canvas ,然后在Notification使用該Bitmap 或者,您可以使用矢量backport庫,但是對於您的少數Notification圖標,為它們生成PNG文件並在舊設備上使用它們。 將相應的矢量drawable放在res/drawable-anydpi-v21/ ,較新的設備將使用矢量drawables,而較舊的設備將回退到PNG。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM