Search code examples
javaandroidandroid-fragmentsandroid-compatibility

Android compatibility package sample FragmentTabsPager crash


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.


Solution

  • 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