java - 未为Notification.builder定义方法build()

标签 java android libraries

我正在疯狂地构建通知。 我在网上读到我需要更新我的库。 我右键单击该项目; proprieties 和我未选中,并且在下载最后一个包后,重新检查了我的库 android.support.v7。 当我重新启动 eclipse 时,我遇到了同样的错误。 我想我不知道如何更新库。请帮帮我 编辑:我将 notification.builder 更改为 notificationcompat.builder() 我可以编译该项目,但现在我有这个错误日志

package com.example.simplenotification;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RemoteViews;

public class MainActivity extends Activity {

 private static final int MY_NOTIFICATION_ID = 0;

 private int mNotificationCount;

 private final CharSequence tickerText ="this is tickerText";
 private final CharSequence contentTitle="this contentTitle";
 private final CharSequence contentText="this coontentText";

 private Intent mNotificationIntent;
 private PendingIntent mContentIntent;

 RemoteViews mContentView = new RemoteViews("com.example.simplenotification.StatusBarWithCustomView", R.layout.custom_view);
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mNotificationIntent = new Intent(getApplicationContext(),AppGet.class);
    mContentIntent = PendingIntent.getActivity(getApplicationContext(), 0, mNotificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
    final Button button = (Button) findViewById(R.id.button1);


    button.setOnClickListener(new OnClickListener() {


        public void onClick(View v){
            mContentView.setTextViewText(R.id.textView1, contentText + "(" + ++mNotificationCount +")");


            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(MainActivity.this)
            .setTicker(tickerText)
            .setSmallIcon(android.R.drawable.stat_notify_more)
            .setAutoCancel(true)
            .setContentIntent(mContentIntent)
            .setContent(mContentView);

            NotificationManagerCompat mNotificationManager = NotificationManagerCompat.from(MainActivity.this);
            mNotificationManager.notify(MY_NOTIFICATION_ID,notificationBuilder.build());
        }


        }



    });



 }

}

错误日志显示:

09-18 11:19:22.344: E/AndroidRuntime(978): FATAL EXCEPTION: main
09-18 11:19:22.344: E/AndroidRuntime(978): android.app.RemoteServiceException: Bad       notification posted from package com.example.simplenotification: Couldn't expand   RemoteViews for: StatusBarNotification(package=com.example.simplenotification id=0 tag=null  notification=Notification(contentView=com.example.simplenotification.StatusBarWithCustomView/0x7f030019 vibrate=null,sound=null,defaults=0x0,flags=0x10) priority=0)
09-18 11:19:22.344: E/AndroidRuntime(978):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1259)
09-18 11:19:22.344: E/AndroidRuntime(978):  at android.os.Handler.dispatchMessage(Handler.java:99)
09-18 11:19:22.344: E/AndroidRuntime(978):  at android.os.Looper.loop(Looper.java:137)
09-18 11:19:22.344: E/AndroidRuntime(978):  at android.app.ActivityThread.main(ActivityThread.java:4340)
09-18 11:19:22.344: E/AndroidRuntime(978):  at java.lang.reflect.Method.invokeNative(Native Method)
09-18 11:19:22.344: E/AndroidRuntime(978):  at java.lang.reflect.Method.invoke(Method.java:511)
09-18 11:19:22.344: E/AndroidRuntime(978):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-18 11:19:22.344: E/AndroidRuntime(978):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-18 11:19:22.344: E/AndroidRuntime(978):  at dalvik.system.NativeStart.main(Native Method)

最佳答案

如果您想使用支持库中的通知生成器,我认为您需要支持库版本 4。尝试导入

android.support.v4.app.NotificationCompat;

并使用:

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getApplicationContext())

我假设您想使用支持版本,因为您正在谈论支持库。否则,如果您想使用普通的通知生成器,我看不到错误在哪里。添加此作为答案,因为我没有足够的代表来发表评论。

关于java - 未为Notification.builder定义方法build(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25907626/

相关文章:

android - Awareness API 需要哪个依赖项?

java - 链接Matlab和Java : Library JMatLink. dll无法加载

Java Eclipse Android 初学者问题 - 如何添加库或链接文件夹。初学者引用

Java 语法数组 {1,2,3} 与 new int[] {1,2,3}

java - 在 MySQL 中插入来自 java 的日期时间字符串

java - 如何在 Android 应用程序中运行 Java jdt AST

android - 在 Android 中从相机捕获后增加图像的亮度和对比度

java - "SQLite JDBC: inconsistent internal state"尝试从 ResultSetMetaData 获取 ColumnCount

Android:如何通过 Intent 在 Facebook 上共享图像和文本?

javascript - 从 Paperscript 到 Javascript