NotificationCompat.Builder不接受第二个参数NotificationComp

2019-05-12 14:05发布

出于某种原因,我NotificationCompat.Builder不会接受第二个参数,我不知道如何解决它。 我看到一些其他的答案,但大多问题是在gradle这个版本,但我是跟上时代的,如下图所示:

if (Build.VERSION.SDK_INT >= 26) {
        Intent notificationIntent = new Intent(this, MainActivity.class);
        PendingIntent mPendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

        Notification mNotification = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setContentTitle("Content Title")
                .setContentText("Content Text")
                .setSmallIcon(R.drawable.ic_check)
                .setContentIntent(mPendingIntent)
                .build();

            startForeground(1, mNotification);
            mNotification.notify();
        }

这些都是我的gradle这个文件

的build.gradle:项目

buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.2'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

的build.gradle:应用

apply plugin: 'com.android.application'

    android {
        compileSdkVersion 26
        defaultConfig {
            applicationId "rs.dreamlight.parkomat"
            minSdkVersion 15
            targetSdkVersion 26
            versionCode 1
            versionName "1.0"
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
            vectorDrawables.useSupportLibrary = true
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    }


    dependencies {
        implementation fileTree(dir: 'libs', include: ['*.jar'])
        implementation 'com.android.support:appcompat-v7:26.1.0'
        implementation 'com.android.support:support-v4:26.1.0'
        implementation 'com.android.support.constraint:constraint-layout:1.0.2'
        implementation 'com.android.support:support-vector-drawable:26.1.0'
        implementation 'com.google.code.gson:gson:2.8.4'
        testImplementation 'junit:junit:4.12'
        androidTestImplementation 'com.android.support.test:runner:1.0.1'
        androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
        implementation 'com.android.support:design:26.1.0'
        implementation 'com.android.support:cardview-v7:26.1.0'
        implementation 'com.github.clans:fab:1.6.4'
    }

有任何想法吗?

Answer 1:

请确保您包括NotificationCompat库的正确版本: import android.support.v4.app.NotificationCompat; 。 这里是通知通道,这是一个新的功能,由于Android的O的官方指南: https://developer.android.com/training/notify-user/channels 。



Answer 2:

TL; DR; 目标依赖的API级别是不正确我build.gradle文件。

完整的细节 :首先,在我的import语句我是指包装的正确版本马特的建议:

import android.support.v4.app.NotificationCompat;

接下来的问题和马特的答案集体给我的指针来解决这是给完全相同编译错误所面临OP我自己的特殊问题。 在我的情况的问题是app模块build.gradle文件。

我原来的依存度如下:

implementation 'com.android.support:appcompat-v7:25.1.0'

我把它改成下面来解决我的错误:

implementation 'com.android.support:appcompat-v7:26.1.0'

从本质上讲,因为我的目标API级别是不正确channelId提到的参数是在API级别26起介绍这里和这里 。

有趣的是,我在logcat的窗口只看到一个编译错误。 在此线程读取信息后,我去结帐我app模块build.gradle在那里我看到了问题的根源文件。 有与下面的错误消息的执行语句显示一个红色波浪线:

错误消息的标题:

这种支持库不应该使用比compileSdkVersion不同版本(25)(26)

错误消息的详细信息:

有图书馆,或工具和库的一些组合,不兼容,或会导致错误。 一个这样的不兼容与版本的Android支持库的编译是不是最新版本(或特别版本比你targetSdkVersion更低)。

为什么我花了一段时间来调试这个问题的原因是,与错误的依赖版本此特定错误没有被任何编译或摇篮同步期间显示在输出日志中的任何地方。 Android Studio中应更及时向开发人员展示这样的错误。



文章来源: NotificationCompat.Builder doesn't accept 2nd argument