我开发了一个 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);
这里有人能帮我解决这个问题吗我在这里遗漏了什么
谢谢你的帮助
最佳答案
addAction()
已添加到 API 级别 16; Android 4.0.4 运行 API 级别 15。从 Android 支持包切换到 NotificationCompat.Builder
,或者如果 Build.VERSION.SDK_INT 则仅调用
.addAction()
>=Build.VERSION_CODES.JELLY_BEAN
关于android - java.lang.NoSuchMethodError : android. 应用程序通知$Builder.addAction,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16523807/