Java android.view.InflateException:二进制XML文件行#0:膨胀类按钮时出错

Java android.view.InflateException:二进制XML文件行#0:膨胀类按钮时出错,java,android,xml,Java,Android,Xml,我正在学习一门关于Audacity的课程,并决定键入所有代码,但在尝试运行应用程序时遇到了一个错误 我得到以下错误: 10-27 19:17:41.871 26679-26679/com.example.vhuhwavho.friendlychat W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x41bbec08) 10-27 19:17:41.871 26679-26679/

我正在学习一门关于Audacity的课程,并决定键入所有代码,但在尝试运行应用程序时遇到了一个错误

我得到以下错误:

        10-27 19:17:41.871 26679-26679/com.example.vhuhwavho.friendlychat W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x41bbec08)
    10-27 19:17:41.871 26679-26679/com.example.vhuhwavho.friendlychat E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.vhuhwavho.friendlychat, PID: 26679
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.vhuhwavho.friendlychat/com.example.vhuhwavho.friendlychat.MainActivity}: android.view.InflateException: Binary XML file line #0: Error inflating class Button
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2334)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2392)
    at android.app.ActivityThread.access$900(ActivityThread.java:169)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:146)
    at android.app.ActivityThread.main(ActivityThread.java:5487)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
    at dalvik.system.NativeStart.main(Native Method)
    Caused by: android.view.InflateException: Binary XML file line #0: Error inflating class Button
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:719)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
    at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
    at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
    at com.example.vhuhwavho.friendlychat.MainActivity.onCreate(MainActivity.java:42)
    at android.app.Activity.performCreate(Activity.java:5451)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2392) 
    at android.app.ActivityThread.access$900(ActivityThread.java:169) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:146) 
    at android.app.ActivityThread.main(ActivityThread.java:5487) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:515) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099) 
    at dalvik.system.NativeStart.main(Native Method) 
    Caused by: java.lang.UnsupportedOperationException: Can't convert to color: type=0x1
    at android.content.res.TypedArray.getColor(TypedArray.java:327)
    at android.support.v7.widget.TintTypedArray.getColor(TintTypedArray.java:163)
    at android.support.v7.widget.ThemeUtils.getThemeAttrColor(ThemeUtils.java:65)
    at android.support.v7.widget.AppCompatDrawableManager.createDefaultButtonColorStateList(AppCompatDrawableManager.java:584)
    at android.support.v7.widget.AppCompatDrawableManager.getTintList(AppCompatDrawableManager.java:536)
    at android.support.v7.widget.AppCompatBackgroundHelper.loadFromAttributes(AppCompatBackgroundHelper.java:53)
    at android.support.v7.widget.AppCompatButton.<init>(AppCompatButton.java:74)
at android.support.v7.widget.AppCompatButton.<init>(AppCompatButton.java:67)
    at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:109)
    at android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1024)
    at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1081)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:690)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:761) 
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:769) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:498) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:398) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:354) 
    at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287) 
    at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139) 
    at com.example.vhuhwavho.friendlychat.MainActivity.onCreate(MainActivity.java:42) 
    at android.app.Activity.performCreate(Activity.java:5451) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2392) 
    at android.app.ActivityThread.access$900(ActivityThread.java:169) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:146) 
    at android.app.ActivityThread.main(ActivityThread.java:5487) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:515) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099) 
    at dalvik.system.NativeStart.main(Native Method) 
    10-27 19:17:46.696 26679-26679/com.example.vhuhwavho.friendlychat I/Process: Sending signal. PID: 26679 SIG: 9                                                                     
以下是
@layout
中的
项\u message.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.vhuhwavho.friendlychat.MainActivity">

    <ListView
        android:id="@+id/messageListView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/linearLayout"
        android:stackFromBottom="true"
        android:divider="@android:color/transparent"
        android:transcriptMode="alwaysScroll"
        tools:listitem="@layout/item_message"/>

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:orientation="horizontal">

        <ImageButton
            android:id="@+id/photoPickerButton"
            android:layout_width="36dp"
            android:layout_height="36dp"
            android:background="@android:drawable/ic_menu_gallery" />

        <EditText
            android:id="@+id/messageEditText"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_weight="1" />

        <Button
            android:id="@+id/sendButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:enabled="false"
            android:text="@string/send_button_label"/>

    </LinearLayout>

    <ProgressBar
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"/>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_marginLeft="@dimen/activity_horizontal_margin"
              android:layout_marginStart="@dimen/activity_horizontal_margin"
              android:orientation="vertical">

    <ImageView
        android:id="@+id/photoImageView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true" />

    <TextView
        android:id="@+id/messageTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:textAppearance="?android:attr/textAppearanceLarge"
        tools:text="Message" />

    <TextView
        android:id="@+id/nameTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:textAppearance="?android:attr/textAppearanceSmall"
        tools:text="Name" />

</LinearLayout>
以下是
@values
中的
colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#FF9800</color>
    <color name="colorPrimaryDark">#E65100</color>
    <color name="colorAccent">#2E7D32</color>
    <color name="colorTitle">#ffffff</color>
</resources>
<resources>
    <string name="app_name">Friendly Chat</string>
    <string name="sign_out">Sign Out</string>
    <string name="send_button_label">Send</string>
</resources>
<resources xmlns:tools="http://schemas.android.com/tools">

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>

        <!--<item name="android:colorButtonNormal">@drawable/button_selector</item>-->
        <item name="colorButtonNormal">@drawable/button_selector</item>
        <item name="android:buttonStyle">@style/FriendlyButtonStyle</item>
    </style>

    <style name="FriendlyButtonStyle" parent="Widget.AppCompat.Button">
        <item name="android:textColor">@color/colorTitle</item>

    </style>
</resources>
<resources>
    <!-- Default screen margins, per the Android Design guidelines. -->
    <dimen name="activity_horizontal_margin">16dp</dimen>
    <dimen name="activity_vertical_margin">16dp</dimen>
</resources>
<resources>
    <!-- Example customization of dimensions originally defined in res/values/dimens.xml
         (such as screen margins) for screens with more than 820dp of available width. This
         would include 7" and 10" devices in landscape (~960dp and ~1280dp respectively). -->
    <dimen name="activity_horizontal_margin">64dp</dimen>
</resources>
以下是
@values
中的
style.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#FF9800</color>
    <color name="colorPrimaryDark">#E65100</color>
    <color name="colorAccent">#2E7D32</color>
    <color name="colorTitle">#ffffff</color>
</resources>
<resources>
    <string name="app_name">Friendly Chat</string>
    <string name="sign_out">Sign Out</string>
    <string name="send_button_label">Send</string>
</resources>
<resources xmlns:tools="http://schemas.android.com/tools">

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>

        <!--<item name="android:colorButtonNormal">@drawable/button_selector</item>-->
        <item name="colorButtonNormal">@drawable/button_selector</item>
        <item name="android:buttonStyle">@style/FriendlyButtonStyle</item>
    </style>

    <style name="FriendlyButtonStyle" parent="Widget.AppCompat.Button">
        <item name="android:textColor">@color/colorTitle</item>

    </style>
</resources>
<resources>
    <!-- Default screen margins, per the Android Design guidelines. -->
    <dimen name="activity_horizontal_margin">16dp</dimen>
    <dimen name="activity_vertical_margin">16dp</dimen>
</resources>
<resources>
    <!-- Example customization of dimensions originally defined in res/values/dimens.xml
         (such as screen margins) for screens with more than 820dp of available width. This
         would include 7" and 10" devices in landscape (~960dp and ~1280dp respectively). -->
    <dimen name="activity_horizontal_margin">64dp</dimen>
</resources>
下面是
@values
中的
dimens.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#FF9800</color>
    <color name="colorPrimaryDark">#E65100</color>
    <color name="colorAccent">#2E7D32</color>
    <color name="colorTitle">#ffffff</color>
</resources>
<resources>
    <string name="app_name">Friendly Chat</string>
    <string name="sign_out">Sign Out</string>
    <string name="send_button_label">Send</string>
</resources>
<resources xmlns:tools="http://schemas.android.com/tools">

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>

        <!--<item name="android:colorButtonNormal">@drawable/button_selector</item>-->
        <item name="colorButtonNormal">@drawable/button_selector</item>
        <item name="android:buttonStyle">@style/FriendlyButtonStyle</item>
    </style>

    <style name="FriendlyButtonStyle" parent="Widget.AppCompat.Button">
        <item name="android:textColor">@color/colorTitle</item>

    </style>
</resources>
<resources>
    <!-- Default screen margins, per the Android Design guidelines. -->
    <dimen name="activity_horizontal_margin">16dp</dimen>
    <dimen name="activity_vertical_margin">16dp</dimen>
</resources>
<resources>
    <!-- Example customization of dimensions originally defined in res/values/dimens.xml
         (such as screen margins) for screens with more than 820dp of available width. This
         would include 7" and 10" devices in landscape (~960dp and ~1280dp respectively). -->
    <dimen name="activity_horizontal_margin">64dp</dimen>
</resources>

请帮助我解决错误。

按钮选择器是问题的原因。。如果您阅读了异常的其余部分,则会提到根本原因。。因为找不到资源id

您必须从以下位置更改button_selector.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="#999999" android:state_enabled="false"/>
    <item android:color="@color/colorAccent" android:state_enabled="true"/>
</selector>

致:



而且它会很好地工作。

也许这对任何人都有帮助>我遇到了类似的崩溃

InflateException:Binary-XML文件行#0:膨胀类EditText时出错

在安卓8.0和8.1版本的apk中 由于xml字体原因,我没有使用最新的支持库,我更新了支持库版本,它已修复

xml具有

<EditText
        android:id="@+id/editEmail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="@font/sanfranciscodisplay_medium"
        android:hint="@string/enter_email_address"
        android:lines="1"
        android:inputType="textEmailAddress"
        android:layout_centerHorizontal="true"/>

在我的例子中,背景XML不在一个可绘制目录中。我将它们全部添加到drawable hdpi文件夹中,然后重建项目。这解决了这个问题


这对我有用

您是否尝试将gradle.properties文件添加到下面这一行

 android.enableAapt2=false
然后通过运行

./gradlew --stop

我遇到了同样的问题,我最终解决了打开gradle.properties并添加以下行的问题:

android.enableAapt2=false

希望这对您有所帮助。

您可以将可绘制文件声明到
v21
v24
文件夹中,以更改此设置,选择
按钮选择器.xml
文件转到“显示在”菜单选项文件选择剪切
按钮选择器.xml
文件,然后移动到可绘制文件夹中并粘贴它。

第42行的内容,
MainActivity.java:42
?因为在发布的
MainActivity
类中有一行:
mUsername=ANONYMOUS与错误无关42。setContentView(R.layout.activity_main);在您发布的代码中,这是第40行,因此缺少2行,请更新好吗?@pleft,完成。您确定这是所有的
logcat
错误吗?也许还有更多的问题,你能发布吗?但问题是这个解决方案对我帮助很大。:)当时,我遇到了同样的问题,当我升级我的Android Studio Gradle时也发生了同样的问题,所以这对我来说很有效。我没有提供确切的链接,所以我在评论中对其进行了更新。该链接已被弃用并删除。不再有效!