9

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

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

在添加之前一切正常ActionBarSherlock(按照本教程)。

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

2 回答 2

21

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

于 2013-03-12T12:44:59.240 回答
0

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

[ 如果使用 ActionBar Sherlock ]

1 从 SDK 更新 Android 支持库

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

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

4 清洁

于 2014-03-01T19:52:51.963 回答