Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.NoSuchMethodError: android.app.Notification$Builder.addAction

I have developed an android application where i have used notification to be displayed when the application starts

The application works perfect when i run the code on android emulator where as when i try to run the same on real device which has android version of 4.0.4

It throws me error in the logcat as

05-13 19:06:45.824: E/AndroidRuntime(15402): FATAL EXCEPTION: main
05-13 19:06:45.824: E/AndroidRuntime(15402): java.lang.NoSuchMethodError: android.app.Notification$Builder.addAction
05-13 19:06:45.824: E/AndroidRuntime(15402):    at com.example.gpstracker.MainActivity.onCreate(MainActivity.java:54)
05-13 19:06:45.824: E/AndroidRuntime(15402):    at android.app.Activity.performCreate(Activity.java:4470)
05-13 19:06:45.824: E/AndroidRuntime(15402):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
05-13 19:06:45.824: E/AndroidRuntime(15402):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
05-13 19:06:45.824: E/AndroidRuntime(15402):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)

The code which i have tried is ,

Notification noti = new Notification.Builder(this)
                .setContentTitle("Driver GPS Tracker Application")
                .setContentText("9ciphers")
                .setSmallIcon(R.drawable.ic_launcher).setContentIntent(in)
                .addAction(R.drawable.ic_launcher, "Start", i)
                .addAction(R.drawable.ic_launcher, "Stop", in)
                .addAction(R.drawable.ic_launcher, "Exit", pIntent).build();
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        // Hide the notification after its selected
        noti.flags |= Notification.FLAG_AUTO_CANCEL;

        notificationManager.notify(0, noti);
        Toast.makeText(getApplicationContext(), "Application Started", Toast.LENGTH_SHORT).show();
        notifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

can anyone here help me to sort this out am i missing something here

thanks for your help

like image 481
NetStarter Avatar asked May 13 '13 13:05

NetStarter


People also ask

What is NotificationCompat builder?

public class NotificationCompat.Builder. Builder class for NotificationCompat objects. Allows easier control over all the flags, as well as help constructing the typical notification layouts. On platform versions that don't offer expanded notifications, methods that depend on expanded notifications have no effect.

What is setContentIntent?

NotificationCompat.Builder setContentIntent (PendingIntent intent) : Supplies a PendingIntent to send when the notification is clicked. NotificationCompat.Builder setContentText (CharSequence text) : Sets the text (second row) of the notification, in a standard notification.


1 Answers

addAction() was added to API Level 16; Android 4.0.4 runs API Level 15. Either switch to NotificationCompat.Builder from the Android Support package, or only call addAction() if Build.VERSION.SDK_INT>=Build.VERSION_CODES.JELLY_BEAN.

like image 154
CommonsWare Avatar answered Sep 28 '22 09:09

CommonsWare