Search code examples
androidandroid-actionbarandroid-support-libraryandroid-actionbar-compat

App crashes when implementing Action Bar support v7 library for android 2.3 and above


I have successfully created an app in Eclipse and it works for android 4.0 and above. Now I want to make it compatible also for android 2.3 and above. In my app I have used action bar, so I have added the android-support-v7-appcompat library project in my workset in my workspace. After following the http://developer.android.com/tools/support-library/setup.html instructions, now I have 'android-support--v7-appcompat.jar' and 'android-support-v4.jar'under my projects Android Private Libraries section. Also I have the jar files under libs folder of my project.

In my Main Activity, I have imported:

import android.support.v7.app.ActionBarActivity;
    import android.support.v7.app.ActionBar;

In AndroidManifest.xml file, I have declared:

<application android:allowBackup="true" 

    android:theme="@style/Theme.AppCompat"

Also, for different activities, declared as:

 <activity android:
                 android:uiOptions="splitActionBarWhenNarrow"

Previously my AndroidManifest.xml was like this:

<application android:allowBackup="true" 

        android:theme="@style/<theme>"

and the styles.xml under res>values folder was like this:

    <resources xmlns:android="http://schemas.android.com/apk/res/android">

       <!--
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
       -->
       <style name="<base>" parent="android:Theme.Light">
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.
        -->
        </style>

        <!-- Application theme. -->
       <style name="<theme>" parent="<base>">
        <!-- All customizations that are NOT specific to a particular API-level can go      here. -->
    </style>

</resources>

used

  <style name="<base>" parent="android:Theme.Holo.Light">
            <!-- API 11 theme customizations can go here. -->
        </style> 

in values-v11 folder and

<style name="<base>" parent="android:Theme.Holo.Light.DarkActionBar">
            <!-- API 11 theme customizations can go here. -->
        </style> 

in values-v14 folder.

I have now included all the styles_base.xml and themes_base.xml files from the library project to my project. Also changed styles.xml file by replacing Theme with .

Every time I run the app, I face crashing, now even for 4.0 and later android versions. The message:

 java.main.NoClassDefFounderError: android.support.v7.appcompat.R$styleable
    at android.support.v7.app.ActionBarActivityDelegate.onCreate

Please Help.


Solution

  • Make sure that the compatibility libraries are checked in Project Properties -> Java Build Path --> Order and Export section like below:

    enter image description here

    Otherwise they will not be included in the generated APK.

    Edit:

    I think you should start over with this as you really shouldn't adding these XMLs from the support lib to your own project.

    1. Remove all the XMLs and jars from support lib
    2. Rebuild a new Android Library Project per the link you gave (follow the "Adding libraries with resources" instruction)
    3. Add the Android Library Project to your project
    4. Clean and rebuild project