NotificationCompat.Builder中的NoClassDefFound

时间:2013-06-01 01:31:33

标签: 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);
        }

问题是,

  • 不推荐使用Notifications类的某些方法和构造函数。
  • 所以,替代这个,&#34; developer.android.com&#34;建议使用 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)

1 个答案:

答案 0 :(得分:14)

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

右键单击项目转到属性 - &gt; Java构建路径。选择订单导出选项卡。确保选中了Android私有库。如果您已引用库项目。同样为图书馆项目做同样的事情。清洁和建设。

还转到android sdk manager并检查你是否安装了android sdk构建工具。这可能没有必要,但要确保安装了android构建工具。

相关问题