【问题标题】:Failed to load AppCompat ActionBar with unknown error in android studio无法在 android studio 中加载 AppCompat ActionBar,出现未知错误
【发布时间】:2017-06-09 04:23:27
【问题描述】:

下面是我的 xml 文件。在预览中有两个错误“无法加载 AppCompat ActionBar 并出现未知错误”和“无法实例化多个类”。如何能够运行应用程序。但是应用程序崩溃并且在 Logcat 中没有显示错误。

这是我的预览截图

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">


    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="506dp" />

    <LinearLayout
        android:id="@+id/layout_main"
        android:orientation="vertical"
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:weightSum="1"
        tools:context=".Activity.MainActivity">


    <LinearLayout
        android:id="@+id/main_layout"
        android:orientation="vertical"
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="0.7"
        tools:context=".Activity.MainActivity">

        <!-- our tablayout to display tabs  -->
        <android.support.design.widget.TabLayout
            android:id="@+id/tabLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="50dp"
            app:tabMode="scrollable"
            app:tabSelectedTextColor="@color/colorBlack"
            app:tabTextColor="@color/colorWhite"
            android:background="@color/colorOran"
            android:minHeight="?attr/actionBarSize"
            />

        <!-- View pager to swipe views -->
        <android.support.v4.view.ViewPager
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

    </LinearLayout>

        <LinearLayout
            android:id="@+id/ll"
            android:layout_width="match_parent"
            android:layout_height="64dp"
            android:layout_alignParentBottom="true"
            android:background="#ffffff"
            android:orientation="horizontal"
            android:layout_weight="0.3"
            android:weightSum="4"
            tools:layout_editor_absoluteX="8dp"
            android:layout_marginBottom="8dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true">

            <ImageView
                android:id="@+id/btn_home"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:src="@drawable/ta_home_icon"
                android:background="@color/colorOran"
                android:layout_marginRight="1dp"
                android:scaleType="fitCenter"/>

            <ImageView
                android:id="@+id/btn_new"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:src="@drawable/ta_todaydeals_icon"
                android:background="@color/colorOran"
                android:layout_marginRight="1dp"
                android:scaleType="fitCenter"/>

            <ImageView
                android:id="@+id/btn_fav"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:src="@drawable/ta_fav_icon"
                android:background="@color/colorOran"
                android:layout_marginRight="1dp"
                android:scaleType="fitCenter"/>

            <ImageView
                android:id="@+id/btn_auth"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:src="@drawable/ta_franchie_icon"
                android:background="@color/colorOran"
                android:scaleType="fitCenter"
                android:layout_marginRight="1dp"
                />

        </LinearLayout>

    </LinearLayout>


    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />


</android.support.v4.widget.DrawerLayout>

下面是appbar_main

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.jeyashri.kitchen.jeyashriskitchen.Activity.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">


        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/colorOran"
            app:popupTheme="@style/AppTheme.PopupOverlay">

            <ImageView
                android:id="@+id/app_logo"
                android:layout_width="80dp"
                android:layout_height="match_parent"
                android:src="@drawable/img"/>

            <ImageView
                android:id="@+id/btn_social"
                android:layout_width="75dp"
                android:layout_height="35dp"
                android:layout_marginRight="1dp"
                android:src="@drawable/share_icon" />

    </android.support.v7.widget.Toolbar>

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_main" />


</android.support.design.widget.CoordinatorLayout>

【问题讨论】:

标签: android android-actionbar


【解决方案1】:

此问题的解决方案取决于您使用的 Android 支持库的版本:

支持库26.0.0-beta2

此 android 支持库版本存在导致上述问题的错误

在您的 Gradle 构建文件中使用:

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

与:

buildToolsVersion '26.0.0' 

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

现在一切都应该正常了。


库版本 28(测试版)

这些新版本似乎又遇到了类似的困难。

在您的res/values/styles.xml 中修改AppTheme 样式

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

<style name="AppTheme" parent="Base.Theme.AppCompat.Light.DarkActionBar">

(注意添加的Base.

或者降级库直到问题得到解决:

implementation 'com.android.support:appcompat-v7:28.0.0-alpha1'

【讨论】:

  • 谢谢!做到了。顺便说一句,对于我们 Android 开发新手来说,应该在 build.gradle(Module: app) 文件上执行更改
  • 我今天才开始。我遇到了这个问题。这个答案要求编译一些东西,我不知道,因为它没有解释如何去做。但是 tanchap 的回答解决了问题。他/她清楚地解释了要做什么。对于像我这样的新手来说,这是一项简单的任务。所以我认为这应该是答案。
  • 我的新错误!当我设置我的新项目时,我忘记取消选中“向后兼容性”选项(我不需要向后兼容性)。我重新创建了这个未选中的项目,一切都很好!
  • 只是好奇:人们是如何解决这样的问题的?我永远不会猜到...
  • 谢谢,但是为什么这个bug在2018年还存在呢?对于 Android 新手,由于这个错误,我无法创建“Hello World”项目。
【解决方案2】:

在这个网站上找到它,它对我有用。 修改 /res/values/styles.xml 来自:

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

到:

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

【讨论】:

  • 你能解释一下为什么会这样吗?它将帮助将来重新访问此内容的人。
  • 2018 年 3 月 1 日确认这解决了我的问题。
  • 请注意,如果在您的 AppTheme 中添加“Base”后它不起作用,只需从布局预览顶部转到主题选项,单击左侧选项中的 Menifest 主题,然后选择您的应用主题。它会自动刷新布局。
  • 这也对我有用。不过,有谁知道为什么这是必要的?我已经离开 Android 好几年了,我无法相信为了让一个简单的项目启动和运行,我必须做多少努力。 (这包括尝试构建/运行 Android Studio 提供的一些示例项目。)
  • 2018 年 7 月 16 日,对我来说也适用于 'com.android.support:appcompat-v7:28.0.0-alpha3'。
【解决方案3】:

使用不同的 appcompact 版本修复了 2018 年 6 月的问题。 将这些代码用于您的项目依赖项...

在 build.gradle(Module: app) 添加这个依赖

implementation 'com.android.support:appcompat-v7:28.0.0-alpha1'

快乐编码... :)

【讨论】:

  • 它在测试版中又被打破了:(
【解决方案4】:

方法一:

定位/res/values/styles.xml

改变

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

<style name="AppTheme" parent="Base.Theme.AppCompat.Light.DarkActionBar">

方法二:

修改模板文件(定位:android-studio/plugins/android/lib/templates/gradle-projects/NewAndroidModule/root/res/values/styles.xml.ftl

改变

backwardsCompatibility!true>Theme.AppCompat<#else><#if

backwardsCompatibility!true>Base.Theme.AppCompat<#else><#if

在 YouTube 上观看解决方案

【讨论】:

    【解决方案5】:

    我也遇到了这个问题,已经解决了 从 res/values/styles.xml

    更改行
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    

    1. &lt;style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"&gt;
    2. &lt;style name="AppTheme" parent="Base.Theme.AppCompat.Light.DarkActionBar"&gt;

    两种解决方案都有效

    【讨论】:

    • 快乐编码 :)
    【解决方案6】:

    开启预览模式

    点击以下链接解决问题

    修复 - 渲染问题找不到以下类:android.support.v7.internal

    转到 appTheme ----> 选择 Holo 主题 ---> 刷新

    https://www.youtube.com/watch?v=4MxBnwpcUjA

    【讨论】:

    • 只是将主题从预览窗格更改为 AppTheme 以外的其他主题
    • 它对我有用,只是改变了 AppTheme 以外的主题
    【解决方案7】:

    Android Studio 3.1.3

    中遇到同样的问题

    只需转到 style.xml 文件

    并替换主题名称

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

    <style name="AppTheme" parent="Base.Theme.AppCompat.Light.DarkActionBar">
    

    然后cleanrebuild项目。这将解决错误。

    【讨论】:

      【解决方案8】:

      替换 implementation 'com.android.support:appcompat-v7:28.0.0-beta01'

      implementation 'com.android.support:appcompat-v7:28.0.0-alpha1' 
      

      在 build.gradle (Module:app) 中。它修复了我在 Android Studio 3.1.3 中的红色标记

      【讨论】:

      • 编码愉快
      • 有没有办法在应用程序级别进行更改?这样就不需要为我拥有的每个项目都复制它。
      【解决方案9】:

      在 android 3.0.0 canary 6 中,您必须将所有 2.6.0 beta2 更改为 beta1(appcompat,design,supportvector)

      【讨论】:

        【解决方案10】:

        试试这个:

        只是改变:

        compile 'com.android.support:appcompat-v7:26.0.0-beta2'
        

        到:

        compile 'com.android.support:appcompat-v7:26.0.0-beta1'
        

        Reference

        【讨论】:

          【解决方案11】:

          这对我有用,我在 Style.xml 中做了以下更改

          更改以下代码:

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

          <style name="AppTheme" parent="Base.Theme.AppCompat.Light.DarkActionBar">
          

          【讨论】:

            【解决方案12】:

            使用这个:

            implementation 'com.android.support:appcompat-v7:26.0.0-beta1'
            implementation 'com.android.support:design:26.0.0-beta1'
            

            而不是

            implementation 'com.android.support:appcompat-v7:26.0.0-beta2'
            implementation 'com.android.support:design:26.0.0-beta2'
            

            在我的例子中,它消除了渲染问题。

            【讨论】:

              【解决方案13】:

              这是解决问题的最低配置。

              使用:

              dependencies {
                  ...
                  implementation 'com.android.support:appcompat-v7:26.1.0'
                  ...
              }
              

              与:

               compileSdkVersion 26
               buildToolsVersion "26.0.1"
              

              并进入位于项目根目录内的 build.gradle 文件:

              buildscript {
                  ...
                  ....
                  dependencies {
                      classpath 'com.android.tools.build:gradle:3.0.1'
                      ...
                      ...
                  }
              }
              

              【讨论】:

                【解决方案14】:

                我也遇到了同样的问题。从布局预览窗口更改主题对我没有帮助。

                帮助我的是将其添加到 styles.xml 中的 Apptheme

                dependencies {
                    implementation 'com.android.support:appcompat-v7:27.0.2'
                    implementation 'com.android.support:design:27.0.2'
                }
                

                它仍然给我错误:“无法解析符号小部件...coordinatorLayout”。然后我更新了我的build.gradle(app)

                dependencies {
                    implementation 'com.android.support:appcompat-v7:27.0.2'
                    implementation 'com.android.support:design:27.0.2'
                }
                

                还有一件事:

                compileSdkVersion 27
                targetSdkVersion 27
                

                【讨论】:

                  【解决方案15】:

                  implementation 'com.android.support:appcompat-v7:28.0.0-alpha3' 也有这个问题。

                  我的解决方案是去File -> Invalidate Caches / Restart -> Invalidate -> Close Project -> 从项目窗口中删除项目 -> Open Project(从项目窗口)。

                  【讨论】:

                    猜你喜欢
                    • 2018-11-29
                    • 1970-01-01
                    • 1970-01-01
                    • 2018-12-12
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 2019-01-16
                    相关资源
                    最近更新 更多