7

我开发了一个 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);

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

感谢您的帮助

4

1 回答 1

16

addAction()已添加到 API 级别 16;Android 4.0.4 运行 API 级别 15。要么从 Android 支持包切换到NotificationCompat.Builder,要么仅调用addAction()if Build.VERSION.SDK_INT>=Build.VERSION_CODES.JELLY_BEAN

于 2013-05-13T13:55:02.187 回答