向活动添加工具栏会导致NoClassDefFoundError

时间:2015-02-04 00:34:19

标签: android android-actionbar android-support-library android-toolbar android-actionbaractivity

我正在尝试为活动添加工具栏。 但是我收到了这个错误:

     java.lang.NoClassDefFoundError: android.support.v7.app.ActionBarActivityDelegateBase$2
        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)

活动扩展了基本活动和基本活动 这是抽象的,我扩展了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>

值-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编译('com.android.support:support-v4:19.1.0'){             force = true         }到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 Manifest:

 <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$5.compute(RenderService.java:708)
at com.android.tools.idea.rendering.RenderService$5.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.access$1700(AndroidLayoutPreviewToolWindowManager.java:82)
at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager$7$1.run(AndroidLayoutPreviewToolWindowManager.java:589)
at com.intellij.openapi.progress.impl.ProgressManagerImpl$2.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$7.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$2.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$1.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)

是什么给出了?

1 个答案:

答案 0 :(得分:16)

我遇到麻烦的原因是所有支持库必须是相同的版本

   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
}

非常简单和奇怪。