Failed to load AppCompat ActionBar with unknown error android studio 3.0

浪尽此生 提交于 2019-12-17 20:51:19

问题


After updating android studio to version 3.0, I can't preview layout of my app, I get the error like:

'Failed to load AppCompat ActionBar with unknown error'.

How can I fix this? but if I run the app on my device phone, its run normally.

This is my Gradle file:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 24
    buildToolsVersion '26.0.2'
    defaultConfig {
        applicationId 'com.halloo'
        minSdkVersion 16
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    productFlavors {
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:24.2.1'
    compile 'com.android.support:design:24.2.1'
    compile 'com.android.support:support-v4:24.2.1'
    testCompile 'junit:junit:4.12'
}

dependencies {
    compile 'com.squareup.okhttp3:okhttp:3.5.0'
}
dependencies {
    compile 'com.android.support:support-v4:24.+'
}
dependencies {
    compile 'com.android.support:cardview-v7:24.0.0'
    compile 'com.android.support:recyclerview-v7:24.0.0'
}

dependencies {
    compile 'com.android.support:support-v4:24.+'
    compile 'junit:junit:4.12'
}

dependencies {
    compile 'com.android.support:support-v4:24.+'
    compile 'com.mikhaellopez:hfrecyclerview:1.0.0'
}

Thank you very much for your time and assistance in this matter.


回答1:


First, you need to use the same version of compileSdkVersion, buildToolsVersion, targetSdkVersion, and support library version. I see that you want to use buildToolsVersion '26.0.2'. So, change all of them to version 26.

Second, you need to clean up your build.gradle. There is no need for duplicate dependencies.

Third, try clean and build your project. As the last resort, try File -> Invalidate Caches/Restart...

Your app build.gradle should be something like this:

apply plugin: 'com.android.application'

android {
  compileSdkVersion 26
  buildToolsVersion '26.0.2'
  defaultConfig {
    applicationId 'com.halloo'
    minSdkVersion 16
    targetSdkVersion 26
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
  }
  buildTypes {
    release {
      minifyEnabled false
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
  }
  productFlavors {
  }
}

dependencies {
  compile fileTree(include: ['*.jar'], dir: 'libs')
  androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
  })
  compile 'com.android.support:appcompat-v7:26.1.0'
  compile 'com.android.support:design:26.1.0'
  compile 'com.android.support:support-v4:26.1.0'
  compile 'com.squareup.okhttp3:okhttp:3.5.0'
  compile 'com.android.support:recyclerview-v7:26.1.0'
  compile 'com.mikhaellopez:hfrecyclerview:1.0.0'
  testCompile 'junit:junit:4.12'
}

You also need to check for your project build.gradle. It should contain build:gradle:3.0.0 (as @dheeraj-joshi has pointing out), something like this:

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

allprojects {
  repositories {
    jcenter()
    mavenCentral()
//    maven { url "https://maven.google.com" }
    google()
  }
}


task clean(type: Delete) {
  delete rootProject.buildDir
}

Then, you need to check your gradle version. It should at least using gradle-4.1.




回答2:


I had similar problem (maybe not exactly the same one).

I was able to run the starter code on the phone and the menu was there. However, preview pane in Android Studio didn't show actual layout.

I have fixed it by changing Theme.AppCompat.Light.DarkActionBar to Base.Theme.AppCompat.Light.DarkActionBar in styles.xml.




回答3:


This is a bug in android support library version "26.0.0-beta2'

use:

compile 'com.android.support:appcompat-v7:24.2.1'

with:

buildToolsVersion '26.0.0' and

classpath

'com.android.tools.build:gradle:3.0.0-alpha8'

everything should work fine.



来源:https://stackoverflow.com/questions/47067706/failed-to-load-appcompat-actionbar-with-unknown-error-android-studio-3-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!