ActionBarSherlock: java.lang.NoClassDefFoundError:

2019-01-17 22:23发布

问题:

I am trying to build a tiny sample application with ActionBarSherlock 4.1 using Eclipse Indigo and ADT r20.

I created a new Android project with a blank activity, copied actionbarsherlock.jar to libs and referenced it in the build path.

The app builds successfully, but upon starting on either the emulator (using 2.2) or the device (using 4.0.4), it crashes with the error:

java.lang.NoClassDefFoundError: com.actionbarsherlock.R$styleable
        at com.actionbarsherlock.view.MenuInflater$MenuState.readItem(MenuInflater.java:328)
        ...

I am not using proguard.

I have tried cleaning the ActionBarSherlock project, copying the new jar into my sample project's libs, and then cleaning my sample project.

My MainActivity.java is quite simple:

package com.example.lrn;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import android.os.Bundle;

public class MainActivity extends SherlockActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getSupportMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

The menu has but a single item:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/menu_settings"
        android:title="@string/menu_settings"
        android:orderInCategory="100"
        android:showAsAction="ifRoom" />
</menu>

And the AndroidManifest.xml is also just about as Eclipse created it:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.lrn"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

At this point I really have no idea what I may have missed.

回答1:

I resolved this issue myself by adding ActionBarSherlock to my Eclipse project in a different way.

Instead of copying actionbarsherlock.jar into libs and adding it to the build path, I added it as a referenced project in the Android section of the project properties.

The app now builds and runs correctly on both the emulator and the device.



回答2:

If you get:

 ActionBarSherlock: java.lang.NoClassDefFoundError:
         com.actionbarsherlock.R$styleable 

exception, then you can fix that by adding Sherlock theme in your manifest file.

android:theme="@style/Theme.Sherlock.Light.DarkActionBar"


回答3:

There's a problem with the latest ADT 21.1 that can cause this error to surface, even if everything else seems correct. You can fix it by downgrading to ADT 21 and the tools r21. Here's a bug report with some more detail:

https://code.google.com/p/android/issues/detail?id=42686



回答4:

Please let your project's package name and the 3rd party lib package name as the same.

e.g. if the 3rd party lib package name is com.actionbarsherlock and your project's package name is com.yourproject it is of course, you will get java.lang.NoClassDefFoundError: com.actionbarsherlock.R$styleable error.

To use the 3rd party lib and not use reference like Michael Hampton's answer, you should follow the steps:

  1. rename the package name of com.actionbarsherlock to com.yourproject( I assume that the package name of your project is com.yourproject. Click right button of the mouse on your project -> Android Tools-> Rename Application Package)

  2. copy com.actionbarsherlock/bin/xxx.jar to com.yourproject/libs

  3. copy or merge the resource from xxx.jar in the com.actionbarsherlock's res, such as values, anim, drawable and etc.

I have encountered the same problem, and use the method above thought by myself successfully. If there's more problem, please ask more. I hope all guys could settle the same problem.



回答5:

make sure ActionBarSherlock project \gen folder has R.java file