Error implementing Support Library Action Bar

2019-01-11 10:06发布

问题:

I'm trying to implement the new v7 Support Library ActionBar. However, I can't get it to run properly.

Here's the exception

07-25 09:30:01.704: E/AndroidRuntime(9175): FATAL EXCEPTION: main
07-25 09:30:01.704: E/AndroidRuntime(9175): java.lang.NoClassDefFoundError: android.support.v7.appcompat.R$styleable
07-25 09:30:01.704: E/AndroidRuntime(9175):     at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:98)
07-25 09:30:01.704: E/AndroidRuntime(9175):     at android.support.v7.app.ActionBarActivityDelegateICS.onCreate(ActionBarActivityDelegateICS.java:58)
07-25 09:30:01.704: E/AndroidRuntime(9175):     at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:98)
07-25 09:30:01.704: E/AndroidRuntime(9175):     at com.cidaut.termoweb.MainActivity.onCreate(MainActivity.java:23)
07-25 09:30:01.704: E/AndroidRuntime(9175):     at android.app.Activity.performCreate(Activity.java:5133)
07-25 09:30:01.704: E/AndroidRuntime(9175):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-25 09:30:01.704: E/AndroidRuntime(9175):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
07-25 09:30:01.704: E/AndroidRuntime(9175):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
07-25 09:30:01.704: E/AndroidRuntime(9175):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-25 09:30:01.704: E/AndroidRuntime(9175):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
07-25 09:30:01.704: E/AndroidRuntime(9175):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-25 09:30:01.704: E/AndroidRuntime(9175):     at android.os.Looper.loop(Looper.java:137)
07-25 09:30:01.704: E/AndroidRuntime(9175):     at android.app.ActivityThread.main(ActivityThread.java:5103)
07-25 09:30:01.704: E/AndroidRuntime(9175):     at java.lang.reflect.Method.invokeNative(Native Method)
07-25 09:30:01.704: E/AndroidRuntime(9175):     at java.lang.reflect.Method.invoke(Method.java:525)
07-25 09:30:01.704: E/AndroidRuntime(9175):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
07-25 09:30:01.704: E/AndroidRuntime(9175):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-25 09:30:01.704: E/AndroidRuntime(9175):     at dalvik.system.NativeStart.main(Native Method)

The problem happens at the super.onCreate(bundle) call at onCreate().

Here's all my theming related XML:

styled.xml

<?xml version="1.0" encoding="utf-8"?>

<resources>
    <style name="Theme.Termoweb" parent="@style/Theme.AppCompat.Light.DarkActionBar">
        <item name="actionBarItemBackground">@drawable/selectable_background_termoweb</item>
        <item name="popupMenuStyle">@style/PopupMenu.Termoweb</item>
        <item name="dropDownListViewStyle">@style/DropDownListView.Termoweb</item>
        <item name="actionBarTabStyle">@style/ActionBarTabStyle.Termoweb</item>
        <item name="actionDropDownStyle">@style/DropDownNav.Termoweb</item>
        <item name="actionBarStyle">@style/ActionBar.Solid.Termoweb</item>
        <item name="actionModeBackground">@drawable/cab_background_top_termoweb</item>
        <item name="actionModeSplitBackground">@drawable/cab_background_bottom_termoweb</item>
        <item name="actionModeCloseButtonStyle">@style/ActionButton.CloseMode.Termoweb</item>
        <!-- Light.DarkActionBar specific -->
        <item name="actionBarWidgetTheme">@style/Theme.Termoweb.Widget</item>
    </style>



    <style name="ActionBar.Solid.Termoweb" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
        <item name="background">@drawable/ab_solid_termoweb</item>
        <item name="backgroundStacked">@drawable/ab_stacked_solid_termoweb</item>
        <item name="backgroundSplit">@drawable/ab_bottom_solid_termoweb</item>
        <item name="progressBarStyle">@style/ProgressBar.Termoweb</item>
    </style>


    <style name="ActionBar.Transparent.Termoweb" parent="@style/Widget.AppCompat.ActionBar">
        <item name="background">@drawable/ab_transparent_termoweb</item>
        <item name="progressBarStyle">@style/ProgressBar.Termoweb</item>
    </style>


    <style name="PopupMenu.Termoweb" parent="@style/Widget.AppCompat.ListView.Menu">
        <item name="popupBackground">@drawable/menu_dropdown_panel_termoweb</item>
    </style>


    <style name="DropDownListView.Termoweb" parent="@style/Widget.AppCompat.ListView.DropDown">
        <item name="listSelector">@drawable/selectable_background_termoweb</item>
    </style>


    <style name="ActionBarTabStyle.Termoweb" parent="@style/Widget.AppCompat.ActionBar.TabView">
        <item name="background">@drawable/tab_indicator_ab_termoweb</item>
    </style>


    <style name="DropDownNav.Termoweb" parent="@style/Widget.AppCompat.Spinner">
        <item name="background">@drawable/spinner_background_ab_termoweb</item>
        <item name="popupBackground">@drawable/menu_dropdown_panel_termoweb</item>
        <item name="dropDownSelector">@drawable/selectable_background_termoweb</item>
    </style>


    <style name="ProgressBar.Termoweb" parent="@style/Widget.AppCompat.ProgressBar.Horizontal">
        <item name="progressDrawable">@drawable/progress_horizontal_termoweb</item>
    </style>


    <style name="ActionButton.CloseMode.Termoweb" parent="@style/Widget.AppCompat.ActionButton.CloseMode">
        <item name="background">@drawable/btn_cab_done_termoweb</item>
    </style>


    <!-- this style is only referenced in a Light.DarkActionBar based theme -->
    <style name="Theme.Termoweb.Widget" parent="@style/Theme.AppCompat">
        <item name="popupMenuStyle">@style/PopupMenu.Termoweb</item>
        <item name="dropDownListViewStyle">@style/DropDownListView.Termoweb</item>
    </style>
</resources>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.cidaut.termoweb"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/Theme.Termoweb" >
        <activity
            android:name="com.cidaut.termoweb.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

回答1:

So, how I solved this problem:

  1. Import support library as a project from "sdk/extras/android/support/v7/appcompat".
  2. Reference library in your project (for Eclipse, "Properties - Android - Add").
  3. Build projects (for Eclipse, "Projects - Build All"). Make sure, you have "android.support.v7.appcompat" in your main project gen folder.
  4. If it doesn't worked - clean and rebuild project.


回答2:

I have done the following and worked for me.

  • Delete the jar from the libs folder.
  • Import the sdk\extras\android\support\v7\appcompat\ project in your eclipse workspace.
  • Ensure that the android-support-v4.jar is the same in your project.
  • Add the appcompat as a library to your project.

Ensure that your in yout Manifest.xml your activity has the correct theme

    <activity
        android:name="***.*****.******"
        android:label="@string/app_name"
        android:theme="@style/Theme.AppCompat.Light"
         >

Good Luck ;)



回答3:

In addition to BoredT's answer I added this line to res/values/styles.xml

<style name="AppTheme" parent="@style/Theme.AppCompat.Light">


回答4:

Follow steps from Adding libraries with resources -> Eclipse http://developer.android.com/tools/support-library/setup.html :

  1. follow all the steps (use copy project in workspace)
  2. add android.library.reference.1=../android-support-v7-appcompat to your project.properties
  3. use android:theme="@style/Theme.AppCompat" I used it in application tag you can apply to individual activities as well

Rebuild all!



回答5:

I think you should change your target version to 18 in the manifest.

android:targetSdkVersion="18"


回答6:

This is how I solve the problem: rebuild the adroid-support-v7-appcompat library project in your eclipse workspace. Then build your own project again.



回答7:

How to import a project as a library in android studio? Follow the tips blog as a reference: http://showlabor.blogspot.com.br/2013/05/actionbarsherlock-in-android-studio-01.html

The next question, also helped a lot: Error implementing Support Library Action Bar

The following image shows the structure of the imported project. That is copied to the root of my project and placed in the directory 'libraries': https://dl.dropboxusercontent.com/u/67269258/Tuto/tree.PNG

In addition to mounting the above structure. In 'settings.glade' I had to add the new project:

include ':FreeMusic', ':libraries:ActionBarCompat:appcompat'

Inside the library project, added the file 'build.gradle' It is located in: libraries/ActionBarCompat/appcompat

 buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.5.+'
    }
}

apply plugin: 'android-library'

dependencies {
    compile files(
        'libs/android-support-v4.jar',
        'libs/android-support-v7-appcompat.jar'
    )
}
android {
    compileSdkVersion 18
    buildToolsVersion "18.0.1"

    defaultConfig {
        minSdkVersion 7
        targetSdkVersion 16
    }
    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }

        instrumentTest.setRoot('tests')
    }
}

In 'build.gradle' (internal project) of my project.

dependencies {
 compile project(':libraries:ActionBarCompat:appcompat')
}

In android manifest added the necessary theme:

<activity
 android:theme="@style/Theme.AppCompat"
 android:name="es.hol.soundmedia.MainActivity"

After all this work. Perhaps an easier way to set this up, but for me the account is working. Unfortunately I can not show the result, because I'm being blocked, but I hope the instructions help.



回答8:

For those that don't like needing manual user effort in Eclipse in order to do a build from checkout, the magical change that Eclipse makes is to add a line to project.properties like:

android.library.reference.1=../android-support-v7-appcompat

This is enough to build your application from ant. You can do this in the source yourself, or via the Eclipse user interface - either way you can check it in to your version control system and successfully build from a fresh checkout of the source without requiring manual steps.

But you can instead make the path a sub-directory of your own project if that suits better, E.g.

android.library.reference.1=android-support-v7-appcompat

... and ...

cp -r $SDK/extras/android/support/v7/appcompat android-support-v7-appcompat

Note that Eclipse will still show various warnings until the sub-directory is also added [manually!] to Eclipse's workspace as a project. But an ant build will successfully build without using Eclipse.



回答9:

Check for R.java for appcompat v7 jar is present in your generated folders of your application