Java 如何在 Android Studio 中导入 android.support.v7.app.NotificationCompat.Builder 类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38249970/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 20:08:25  来源:igfitidea点击:

How to import android.support.v7.app.NotificationCompat.Builder class in Android Studio

javaandroidandroid-studio

提问by Kuldeep Kumar

I am trying to implement simple notifications in my android app. I am reffering this developer guide

我正在尝试在我的 android 应用中实现简单的通知。我正在引用此开发人员指南

But getting this error message :

但是收到此错误消息:

Incompatible types.
Required: android.support.v7app.NotificationCompat.Builder
Found: android.support.v4.app.Notification.Compat.Builder

Error Message screenshot

错误信息截图

For the following code snippet :

对于以下代码片段:

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
                        .setSmallIcon(R.drawable.ic_launcher)
                        .setContentTitle("My notification")
                        .setContentText("Hello World!");

Here are my imports :

这是我的进口:

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.app.NotificationCompat;
import android.view.View;
import android.widget.Button;

I think the correct NotificationCompatclass is imported. I am using Android Studio v2.1.2for development. Please help me with this error message. I am new to android programming and java.

我认为NotificationCompat导入了正确的类。我正在Android Studio v2.1.2用于开发。请帮助我解决此错误消息。我是 android 编程和 java 的新手。

采纳答案by Mephoros

The return type of those builder methods are returning the v4 version of NotificationCompat.Builder. The v7 NotificationCompat.Builder extends the v4 version and largely just inherits the methods from it, meaning the return types don't change.

这些构建器方法的返回类型返回的是 NotificationCompat.Builder 的 v4 版本。v7 NotificationCompat.Builder 扩展了 v4 版本并且很大程度上只是继承了它的方法,这意味着返回类型不会改变。

Documentation:

文档:

If you need the v7 version (for the support of NotificationCompat.MediaStyle), just cast to it.

如果您需要 v7 版本(为了支持 NotificationCompat.MediaStyle),只需投射到它。

NotificationCompat.Builder mBuilder = (android.support.v7.app.NotificationCompat.Builder) new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.ic_launcher)
                    .setContentTitle("My notification")
                    .setContentText("Hello World!");

If not, swap your imports to use the v4 version.

如果没有,请交换您的导入以使用 v4 版本。

回答by Dhruvi

Replace

代替

 import android.support.v7.app.NotificationCompat;

with

 import android.support.v4.app.NotificationCompat;

回答by Gopal Meena

Latest working solution 2020

最新的工作解决方案 2020

If you have updated to Androidx then
replace

如果您已更新到 Androidx,则
替换

import android.support.v4.app.NotificationCompat
//or
import android.support.v7.app.NotificationCompat

with

import androidx.core.app.NotificationCompat;