添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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

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 NotificationCompat class is imported. I am using Android Studio v2.1.2 for development. Please help me with this error message. I am new to android programming and java.

As far as I can tell there is no import android.support.v7.app.NotificationCompat; where have you found that? – Sobvan Jun 1, 2018 at 12:21 @Sobvan Exactly, I've been searching this package name at developer.android.com and there is no such thing – Hossein Shahdoost Sep 30, 2018 at 13:03

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.

Documentation:

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

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.

Your documentation link for v7 is broken - it just goes to an index which has an entry for the v4 NotificationCompat, and also an entry that looks like it should be for the v7 NotificationCompat but actually just links back to the same index... – Jules May 13, 2018 at 15:09
import android.support.v4.app.NotificationCompat
import android.support.v7.app.NotificationCompat
import androidx.core.app.NotificationCompat;
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.