NotificationCompact.Builder和ActionBarSherlock的问题

时间:2013-03-12 12:33:41

标签: android compiler-errors notifications actionbarsherlock

在以下代码中,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
    }
}

2 个答案:

答案 0 :(得分:21)

在新版Android支持包中添加了

build()。根据您获得和设置ActionBarSherlock的方式,您可能正在使用旧版Android支持包。确保您在SDK Manager中下载了最新版本,然后在ActionBarSherlock项目和主应用程序项目中使用android-support-v4.jar

答案 1 :(得分:0)

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

[如果使用ActionBar Sherlock]

1从SDK更新Android支持库

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

中的引用

3对sherlockactionbar项目执行相同操作。小心,如果你有一个android-support2-v4.jar,删除它并只添加android-support-v4.jar

4清洁