【问题标题】:NotificationCompat.Builder doesn't accept 2nd argumentNotificationCompat.Builder 不接受第二个参数
【发布时间】:2018-06-08 17:44:44
【问题描述】:

由于某种原因,我的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:app

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'
    }

有什么想法吗?

【问题讨论】:

    标签: android gradle notifications


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      完整细节:首先,在我的导入声明中,我指的是马特建议的正确版本的包:

      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 之后引入的,如 herehere 所述。

      有趣的是,我在 Logcat 窗口中只看到了一个编译错误。在阅读了此线程中的详细信息后,我查看了我的app 模块的build.gradle 文件,在那里我看到了问题的根本原因。执行语句显示了一个红色波浪线,并显示以下错误消息:

      错误信息标题:

      此支持库不应使用与 compileSdkVersion (26)

      错误信息详情:

      有一些库或工具和库的组合, 不兼容,或者可能导致错误。一种这样的不兼容性是 使用不支持的 Android 支持库版本进行编译 最新版本(或者特别是低于您的 targetSdkVersion)。

      我花了一段时间来调试这个问题的原因是,在构建或 Gradle 同步期间,与错误依赖版本相关的这个特定错误没有显示在输出日志的任何地方。 Android Studio 应该更及时地向开发者显示此类错误。

      【讨论】:

      • 面临同样的问题..尝试了上面提到的所有解决方案仍然无法解决问题。
      • @ice spirit你找到解决办法了吗?
      • 当我导入“import android.support.v4.app.NotificationCompat”时它解决了。
      猜你喜欢
      • 2019-08-10
      • 2018-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-12
      • 2023-03-19
      相关资源
      最近更新 更多