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

时间:2013-05-13 13:52:57

标签: android android-notifications

我开发了一个Android应用程序,我在应用程序启动时使用了通知

当我在Android模拟器上运行代码时,应用程序工作正常,当我尝试在Android版本为4.0.4的真实设备上运行相同时

它在logcat中抛出了我的错误

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)

我试过的代码是,

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);

这里的任何人都可以帮我解决这个问题吗?我在这里错过了什么

感谢您的帮助

1 个答案:

答案 0 :(得分:16)

API级别16已添加

addAction(); Android 4.0.4运行API级别15.要么从Android支持包切换到NotificationCompat.Builder,要么只在addAction()时调用Build.VERSION.SDK_INT>=Build.VERSION_CODES.JELLY_BEAN