将工具栏添加到 activity 导致 NoClassDefFoundError

Adding toolbar to activity causes NoClassDefFoundError

我正在尝试将工具栏添加到 activity。 但我收到此错误:

     java.lang.NoClassDefFoundError: android.support.v7.app.ActionBarActivityDelegateBase
        at android.support.v7.app.ActionBarActivityDelegateBase.ensureSubDecor(ActionBarActivityDelegateBase.java:304)
        at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:225)
        at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102)
        at com.loqooapps.Startup.Startup.onCreate(Startup.java:198)
        at android.app.Activity.performCreate(Activity.java:5933)

activity 扩展了一个 Base Activity 并且在 Base activity 这是抽象的,我扩展 ActionBarActivity.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
    setContentView(R.layout.startup);
    ButterKnife.inject(this);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    if(toolbar != null) {
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle("DEMO");
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
    baseApp = (App) this.getApplicationContext();
    yumeInterface = YuMeInterface.getYuMeInterface();
    yumeInterface.setHomeView(this);
    boolean bInitSuccess = yumeInterface.initYuMeSDK();
    if (!bInitSuccess) {
        Log.e(TAG, "Error Initializing YuMe SDK.");
    }
 }

 public abstract class MyBaseActivity extends ActionBarActivity{...}
    compile 'com.android.support:multidex:1.0.0'
    compile 'com.android.support:appcompat-v7:21.0.1'
    compile 'com.android.support:cardview-v7:21.0.3'
    compile 'com.android.support:recyclerview-v7:21.0.3'

这是我的 xml:

 <android.support.v7        compile 'com.android.support:multidex:1.0.0'
    compile 'com.android.support:appcompat-v7:21.0.1'
    compile 'com.android.support:cardview-v7:21.0.3'
    compile 'com.android.support:recyclerview-v7:21.0.3'.widget.Toolbar
    android:id        compile('com.android.support:support-v4:19.1.0') {
        force = true
    }="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    android:background="@color/red"
    android:elevation="3dp"
</android.support.v7.widget.Toolbar>

这是我的 styles.xml:

<!-- Base application theme. -->
<style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowActionBarOverlay">false</item>
    <item name="android:windowBackground">@null</item>
    <item name="android:windowDisablePreview">false</item>
    <item name="android:windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
</style>

values-v21/styles.xml:

<style name="        compile('com.android.support:support-v4:19.1.0') {
        force = true
    }AppTheme" parent="AppBaseTheme">
</style>

    compile('com.android.support:support-v4:19.1.0') {
        force = true
    }

minsdk设置为16 和 目标是 se compile('com.android.support:support-v4:19.1.0') { 力 = 真 }t 到 21

build.gradle

    compile 'com.android.support:multidex:1.0.0'
    compile 'com.android.support:appcompat-v7:21.0.3'
    compile 'com.android.support:cardview-v7:21.0.3'
    compile 'com.android.support:recyclerview-v7:21.0.3'
    compile('com.android.support:support-v4:19.1.0') {
        force = true
    }

Android 清单:

 <activity
        android:name=".Startup.Startup"
        android:screenOrientation="portrait"
        android:theme="@style/AppBaseTheme"

xml中的错误:

  java.lang.NoSuchMethodError: android.support.v4.content.ContextCompat.getDrawable(Landroid/content/Context;I)Landroid/graphics/drawable/Drawable;
at android.support.v7.internal.widget.TintManager.getDrawable(TintManager.java:133)
at android.support.v7.internal.widget.TintTypedArray.getDrawable(TintTypedArray.java:62)
at android.support.v7.widget.Toolbar.<init>(Toolbar.java:249)
at android.support.v7.widget.Toolbar.<init>(Toolbar.java:191)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at org.jetbrains.android.uipreview.ViewLoader.createNewInstance(ViewLoader.java:396)
at org.jetbrains.android.uipreview.ViewLoader.loadView(ViewLoader.java:100)
at com.android.tools.idea.rendering.LayoutlibCallback.loadView(LayoutlibCallback.java:176)
at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:207)
at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:132)
at android.view.LayoutInflater_Delegate.parseInclude(LayoutInflater_Delegate.java:117)
at android.view.LayoutInflater.parseInclude(LayoutInflater.java:858)
at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:802)
at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:64)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:782)
at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:809)
at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:64)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:782)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:385)
at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:400)
at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:332)
at com.android.ide.common.rendering.LayoutLibrary.createSession(LayoutLibrary.java:350)
at com.android.tools.idea.rendering.RenderService.compute(RenderService.java:708)
at com.android.tools.idea.rendering.RenderService.compute(RenderService.java:697)
at com.intellij.openapi.application.impl.ApplicationImpl.runReadAction(ApplicationImpl.java:932)
at com.android.tools.idea.rendering.RenderService.createRenderSession(RenderService.java:697)
at com.android.tools.idea.rendering.RenderService.render(RenderService.java:816)
at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager.doRender(AndroidLayoutPreviewToolWindowManager.java:646)
at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager.access00(AndroidLayoutPreviewToolWindowManager.java:82)
at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager.run(AndroidLayoutPreviewToolWindowManager.java:589)
at com.intellij.openapi.progress.impl.ProgressManagerImpl.run(ProgressManagerImpl.java:178)
at com.intellij.openapi.progress.ProgressManager.executeProcessUnderProgress(ProgressManager.java:209)
at com.intellij.openapi.progress.impl.ProgressManagerImpl.executeProcessUnderProgress(ProgressManagerImpl.java:212)
at com.intellij.openapi.progress.impl.ProgressManagerImpl.runProcess(ProgressManagerImpl.java:171)
at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager.run(AndroidLayoutPreviewToolWindowManager.java:584)
at com.intellij.util.ui.update.MergingUpdateQueue.execute(MergingUpdateQueue.java:320)
at com.intellij.util.ui.update.MergingUpdateQueue.execute(MergingUpdateQueue.java:310)
at com.intellij.util.ui.update.MergingUpdateQueue.run(MergingUpdateQueue.java:254)
at com.intellij.util.ui.update.MergingUpdateQueue.flush(MergingUpdateQueue.java:269)
at com.intellij.util.ui.update.MergingUpdateQueue.flush(MergingUpdateQueue.java:227)
at com.intellij.util.ui.update.MergingUpdateQueue.run(MergingUpdateQueue.java:217)
at com.intellij.util.concurrency.QueueProcessor.runSafely(QueueProcessor.java:238)
at com.intellij.util.Alarm$Request.run(Alarm.java:327)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)

什么给了?

让我烦恼的原因是所有的支持库必须是相同的版本

   compile 'com.android.support:multidex:1.0.0'
    compile 'com.android.support:appcompat-v7:21.0.3'
    compile 'com.android.support:cardview-v7:21.0.3'
    compile ('com.android.support:recyclerview-v7:21.0.3') {
        exclude group: 'com.android.support', module: 'support-v4'
    }
    //compile 'com.android.support:leanback-v17:21.0.3'
    compile 'com.android.support:mediarouter-v7:21.0.3'
    compile('com.android.support:support-v4:21.0.3') {
        force = true
    }

上面我用的是....

 compile('com.android.support:support-v4:19.1.0') {
    force = true
}

很简单也很奇怪。