java.lang.NoSuchMethodError: android.app.Notificat

2019-01-26 12:53发布

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

1条回答
forever°为你锁心
2楼-- · 2019-01-26 13:08

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.

查看更多
登录 后发表回答