Android 兼容性包示例 FragmentTabsPager 崩溃

发布于 2024-12-16 17:31:09 字数 2126 浏览 2 评论 0原文

我尝试了 Android 兼容包提供的示例。 FragmentTabsPager 崩溃并出现以下错误:

11-17 19:00:27.414: E/AndroidRuntime(5401): FATAL EXCEPTION: main
11-17 19:00:27.414: E/AndroidRuntime(5401): java.lang.NoClassDefFoundError: com.example.android.supportv4.app.LoaderCustomSupport$AppListFragment
11-17 19:00:27.414: E/AndroidRuntime(5401):     at com.example.android.supportv4.app.FragmentTabsPager.onCreate(FragmentTabsPager.java:60)
11-17 19:00:27.414: E/AndroidRuntime(5401):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-17 19:00:27.414: E/AndroidRuntime(5401):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2633)
11-17 19:00:27.414: E/AndroidRuntime(5401):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2685)
11-17 19:00:27.414: E/AndroidRuntime(5401):     at android.app.ActivityThread.access$2300(ActivityThread.java:126)
11-17 19:00:27.414: E/AndroidRuntime(5401):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2038)
11-17 19:00:27.414: E/AndroidRuntime(5401):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-17 19:00:27.414: E/AndroidRuntime(5401):     at android.os.Looper.loop(Looper.java:123)
11-17 19:00:27.414: E/AndroidRuntime(5401):     at android.app.ActivityThread.main(ActivityThread.java:4633)
11-17 19:00:27.414: E/AndroidRuntime(5401):     at java.lang.reflect.Method.invokeNative(Native Method)
11-17 19:00:27.414: E/AndroidRuntime(5401):     at java.lang.reflect.Method.invoke(Method.java:521)
11-17 19:00:27.414: E/AndroidRuntime(5401):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
11-17 19:00:27.414: E/AndroidRuntime(5401):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
11-17 19:00:27.414: E/AndroidRuntime(5401):     at dalvik.system.NativeStart.main(Native Method)

如何修复它?

谢谢。

UPD 只是想知道为什么有人投票支持关闭? 我用 Google 搜索了此内容,并在此处找到了已打开的问题。还没有答案。我认为我的问题很合理,值得关注。

I tried sample provided with Android compatibility package. FragmentTabsPager crashes with following error:

11-17 19:00:27.414: E/AndroidRuntime(5401): FATAL EXCEPTION: main
11-17 19:00:27.414: E/AndroidRuntime(5401): java.lang.NoClassDefFoundError: com.example.android.supportv4.app.LoaderCustomSupport$AppListFragment
11-17 19:00:27.414: E/AndroidRuntime(5401):     at com.example.android.supportv4.app.FragmentTabsPager.onCreate(FragmentTabsPager.java:60)
11-17 19:00:27.414: E/AndroidRuntime(5401):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-17 19:00:27.414: E/AndroidRuntime(5401):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2633)
11-17 19:00:27.414: E/AndroidRuntime(5401):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2685)
11-17 19:00:27.414: E/AndroidRuntime(5401):     at android.app.ActivityThread.access$2300(ActivityThread.java:126)
11-17 19:00:27.414: E/AndroidRuntime(5401):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2038)
11-17 19:00:27.414: E/AndroidRuntime(5401):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-17 19:00:27.414: E/AndroidRuntime(5401):     at android.os.Looper.loop(Looper.java:123)
11-17 19:00:27.414: E/AndroidRuntime(5401):     at android.app.ActivityThread.main(ActivityThread.java:4633)
11-17 19:00:27.414: E/AndroidRuntime(5401):     at java.lang.reflect.Method.invokeNative(Native Method)
11-17 19:00:27.414: E/AndroidRuntime(5401):     at java.lang.reflect.Method.invoke(Method.java:521)
11-17 19:00:27.414: E/AndroidRuntime(5401):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
11-17 19:00:27.414: E/AndroidRuntime(5401):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
11-17 19:00:27.414: E/AndroidRuntime(5401):     at dalvik.system.NativeStart.main(Native Method)

How do I fix it?

Thanks.

UPD
Just want to know why does anybody vote for closing?
I googled this and found opened issue here. No answer yet. I think that my question is resonable and deserves attention.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅唱々樱花落 2024-12-23 17:31:09

这里有什么问题吗?
您会收到 NoClassDefFoundError。什么时候发生这种情况:

当前执行时搜索到的类定义已存在
类已编译,但无法再找到定义。

来自 Oracle 文档

您的类或其部分在编译时可用,但在运行时不再可用。当您的 Android 项目设置为某个目标 API 级别并且您使用它的可用类和方法时,就会发生这种情况。然后,您在 Android 版本较低的设备上运行该项目(假设您的 minSdk 设置为允许在 AndroidManifest 中执行此操作 - 该示例项目的默认值为 4) 。突然间,其中一些组件可能不可用。你会得到这个错误。

如何解决?
将您的项目目标 SDK 更改为您想要支持的版本。当您这样做时,您应该会在您选择的工具(例如 eclipse)中看到错误标记和编译问题。
如果将其设置为 2.2。例如,您会看到此类使用 OnQueryTextListenerSearchViewArrayAdapter.addAll() (全部在 API 11 中引入)。这些在 API 级别 10 或更低的设备上不可用。
为此功能构建解决方法应该可以解决该问题。无论如何,考虑到这是示例代码,您应该记住这些方法不可用。

TL;博士:
示例项目使用级别 11 中的 API 功能,这些功能不在兼容性包中。当在运行版本低于 11 的设备上执行时,会抛出此错误。

1 在本例中是一个接口,OnQueryTextListener

What's the problem here?
You get a NoClassDefFoundError. When does that happen:

The searched-for class definition existed when the currently executing
class was compiled, but the definition can no longer be found.

from the oracle documentation

Your class or parts of it were available at compile time, but are no longer available at runtime. That happens when your Android project is set to a certain target API level and you use it's available classes and methods. Then you run that project on a device with a lower Android version (assuming your minSdk is set to allow this in your AndroidManifest - which is 4 by default on that sample project). Suddenly, some of these components might not be available¹. You'll get this error.

How to fix it?
Change your projects target SDK to a version that you want to support. When you do that you should see error markers and compile problems in your tool of choice, e.g. eclipse.
If you set it to 2.2. for example, you'll see that this class uses OnQueryTextListener, SearchView and ArrayAdapter.addAll() (all introduced in API 11). These are not available on a device with API level 10 or lower.
Building a workaround for this functionality should fix the issue. Anyway, considering this is sample code you should just keep in mind that these methods are not available.

TL;DR:
The sample project uses API features from level 11 which are not in the compatibility package. When executed on device running a lower version than 11, this error gets thrown.

¹ In this case an interface, OnQueryTextListener

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文