【问题标题】:Issue with NotificationCompact.Builder and ActionBarSherlockNotificationCompact.Builder 和 ActionBarSherlock 的问题
【发布时间】:2013-03-12 12:33:40
【问题描述】:

在下面的代码中,Eclipse 发现了一个错误:

The method build() is undefined for the type NotificationCompat.Builder

在添加 ActionBarSherlock(在 this tutorial 之后)之前一切正常。

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.TaskStackBuilder;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;

public class NotificationActivity extends BroadcastReceiver {

    NotificationManager nm;

    @Override
    public void onReceive(Context context, Intent intent) {
        nm = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        int notifyID = 1;
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                context)
                .setSmallIcon(R.drawable.zcicon)
                .setAutoCancel(true)
                .setDefaults(
                        Notification.DEFAULT_SOUND
                                | Notification.DEFAULT_LIGHTS)
                .setTicker("mytitle").setContentTitle("mycontent")
                .setContentText("text, text");
        Intent resultIntent = new Intent(context, CalcareReader.class);
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
        // Adds the back stack for the Intent (but not the Intent itself)
        stackBuilder.addParentStack(MyActivity.class);
        // Adds the Intent that starts the Activity to the top of the stack
        stackBuilder.addNextIntent(resultIntent);
        PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
                PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilder.setContentIntent(resultPendingIntent);

        nm.notify(notifyID, mBuilder.build()); // error here
    }
}

【问题讨论】:

    标签: android compiler-errors notifications actionbarsherlock


    【解决方案1】:

    build() 已添加到较新版本的 Android 支持包中。根据您获取和设置 ActionBarSherlock 的方式,您可能使用的是旧版本的 Android 支持包。确保您在 SDK 管理器中下载了最新版本,然后在 ActionBarSherlock 项目和主应用程序项目中使用该 android-support-v4.jar

    【讨论】:

    • 工作谢谢!! link
    • >>"ActionBarSherlock 项目和您的主应用程序项目中的 android-support-v4.jar。"其实没有。在 ActionBarSherlock 项目中您只需要一个。
    • @DoctororDrive:是的。但是,如果旧 JAR 存在于两个项目中,则仅将其替换为一个将导致构建错误。两个项目都需要相同的 JAR,或者只是在 ActionBarSherlock 中有 JAR,而在主项目中没有 JAR。
    • @CommonsWare 这就是我的意思。最新的 jar 应该只在一个需要它的库项目中,或者在所有需要它的库项目中,并且不引用其他拥有它的库项目。
    • @DoctororDrive:你的方法当然是有效的。
    【解决方案2】:

    build() 来自旧版本的 android-support-v4.jar

    [如果使用 ActionBar Sherlock ]

    1 从 SDK 更新 Android 支持库

    2 复制粘贴到您的 lib/ 文件夹,或更新路径中的引用

    3 对 sherlockactionbar 项目执行相同的操作。请注意,如果您有 android-support2-v4.jar,请将其删除并仅添加 android-support-v4.jar

    4 清洁

    【讨论】:

      猜你喜欢
      • 2012-11-06
      • 1970-01-01
      • 2012-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多