9

In the following code, Eclipse found an error:

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

Everything worked fine before adding the ActionBarSherlock (following 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 Answers2

21

build() was added in a newer edition of the Android Support package. Depending on how you obtained and set up ActionBarSherlock, you may be using an older edition of the Android Support package. Make sure that you have the latest one downloaded in your SDK Manager, then use that android-support-v4.jar in both the ActionBarSherlock project and your main application project.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Works thank you!! [link](http://stackoverflow.com/questions/12793722/error-on-add-library-project-in-eclipse) – Stefano Paolessi Mar 12 '13 at 13:08
  • >>"android-support-v4.jar in both the ActionBarSherlock project and your main application project." Actually no. You only need one in the ActionBarSherlock project. – Yaroslav Mytkalyk Mar 12 '13 at 13:56
  • @DoctororDrive: True. However, if the old JAR exists in both projects, replacing it in only one will result in build errors. Either both projects need the same JAR, or just have the JAR in ActionBarSherlock and not have the JAR in the main project. – CommonsWare Mar 12 '13 at 16:02
  • @CommonsWare that's what I mean. The latest jar should be only in one library project that needs it, or in all library projects that need it and do not reference the other library projects that have it. – Yaroslav Mytkalyk Mar 12 '13 at 16:16
  • @DoctororDrive: Yours is certainly a valid approach. – CommonsWare Mar 12 '13 at 16:29
  • Ok, now I delete android-support-v4.jar from my project and it still works, but to fix the previous issue I had follow the tip @Commonsware – Stefano Paolessi Mar 12 '13 at 19:03
  • @CommonsWare : you really rocks man ! i found you the best answerer on stackoverflow ! – Hardik Thaker May 13 '13 at 15:46
0

build() is from an older version of android-support-v4.jar

[ If using ActionBar Sherlock ]

1 Update the Android Support Library from SDK

2 Copy paste this to your lib/ folder, or update the reference at the Path

3 Do the same with the sherlockactionbar project. Be careful, if you have an android-support2-v4.jar, delete it and add only the android-support-v4.jar

4 Clean