NotificationCompat.Builder中的NoClassDefFound

Chi*_*oni 5 android android-notifications

这个概念是在特定时间获得通知.显然,我做到了,直到我将支持版本低于HoneyComb及高于它.

我已经设置了最低版本的SDK 8和目标SDK 17.由于类编码要大得多,我只显示存在问题的核心区域:

int currentapiVersion = android.os.Build.VERSION.SDK_INT;
        Notification notification;
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                new Intent(this, TaskDetails.class), 0);

        if (currentapiVersion < android.os.Build.VERSION_CODES.HONEYCOMB) {

            notification = new Notification(icon, text, time);
            notification.setLatestEventInfo(this, title, text, contentIntent);
            notification.flags |= Notification.FLAG_AUTO_CANCEL;
            mNM.notify(NOTIFICATION, notification);
        } else {
            NotificationCompat.Builder builder = new NotificationCompat.Builder(
                    this);
            notification = builder.setContentIntent(contentIntent)
                    .setSmallIcon(icon).setTicker(text).setWhen(time)
                    .setAutoCancel(true).setContentTitle(title)
                    .setContentText(text).build();

            mNM.notify(NOTIFICATION, notification);
        }
Run Code Online (Sandbox Code Playgroud)

问题是,

  • 不推荐使用Notifications类的某些方法和构造函数.
  • 所以,替代这个,"developer.android.com"建议使用 Notification.Builder.
  • 但是,该类Notification.Builder具有包含在API级别11中的方法(因此在行中显示错误"Call requires API level 11 or more").
  • 所以,它不允许我运行该项目.
  • 经过更多的谷歌搜索,给了我使用类的解决方案 NotificationCompat.Builder.

最后,到达没有发现错误的阶段,我在Sony Xperia Tipo Dual ST21i2上运行我的项目...

悲剧结局:我收到以下错误日志:

06-01 06:34:14.199: E/AndroidRuntime(4178): FATAL EXCEPTION: main
06-01 06:34:14.199: E/AndroidRuntime(4178): java.lang.NoClassDefFoundError: android.support.v4.app.NotificationCompat$Builder
06-01 06:34:14.199: E/AndroidRuntime(4178):     at com.todotaskmanager.service.NotifyService.showNotification(NotifyService.java:99)
06-01 06:34:14.199: E/AndroidRuntime(4178):     at com.todotaskmanager.service.NotifyService.onStartCommand(NotifyService.java:68)
06-01 06:34:14.199: E/AndroidRuntime(4178):     at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2359)
06-01 06:34:14.199: E/AndroidRuntime(4178):     at android.app.ActivityThread.access$1900(ActivityThread.java:123)
06-01 06:34:14.199: E/AndroidRuntime(4178):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
06-01 06:34:14.199: E/AndroidRuntime(4178):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-01 06:34:14.199: E/AndroidRuntime(4178):     at android.os.Looper.loop(Looper.java:137)
06-01 06:34:14.199: E/AndroidRuntime(4178):     at android.app.ActivityThread.main(ActivityThread.java:4424)
06-01 06:34:14.199: E/AndroidRuntime(4178):     at java.lang.reflect.Method.invokeNative(Native Method)
06-01 06:34:14.199: E/AndroidRuntime(4178):     at java.lang.reflect.Method.invoke(Method.java:511)
06-01 06:34:14.199: E/AndroidRuntime(4178):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:817)
06-01 06:34:14.199: E/AndroidRuntime(4178):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
06-01 06:34:14.199: E/AndroidRuntime(4178):     at dalvik.system.NativeStart.main(Native Method)
Run Code Online (Sandbox Code Playgroud)

Chi*_*oni 14

好的......我解决了我的问题.

右键单击项目goto属性 - > Java Build Path.选择订单导出选项卡.确保选中了Android私有库.如果您已引用库项目.同样为图书馆项目做同样的事情.清洁和建设.

也转到android sdk管理器并检查你是否安装了android sdk构建工具.这可能没有必要但请确保安装了android构建工具.

  • 谢谢,在我的情况下它是v4 android库复选框:) (2认同)