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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46979077/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 02:31:57  来源:igfitidea点击:

android.view.InflateException: Binary XML file line #0: Error inflating class Button

javaandroidxml

提问by Yeezus

I am following a course on Audacity, and decided to type out all the code, but then I run into an error when I try run the app.

我正在学习有关 Audacity 的课程,并决定输入所有代码,但是当我尝试运行该应用程序时遇到了错误。

I get the following error:

我收到以下错误:

        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.access0(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.access0(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.access0(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                                                                     

Here's the main activity:

这是主要活动:

package com.example.vhuhwavho.friendlychat;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.ProgressBar;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    public static final String ANONYMOUS = "anonymous";
    public static final int DEFAULT_MSG_LENGTH_LIMIT = 1000;

    private ListView mMessageListView;
    private MessageAdapter mMessageAdapter;
    private ProgressBar mProgressBar;
    private ImageButton mPhotoPickerButton;
    private EditText mMessageEditText;
    private Button mSendButton;

    private String mUsername;


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mUsername = ANONYMOUS;

        // Initialize references to views
        mProgressBar = (ProgressBar) findViewById(R.id.progressBar);
        mMessageListView = (ListView) findViewById(R.id.messageListView);
        mPhotoPickerButton = (ImageButton) findViewById(R.id.photoPickerButton);
        mMessageEditText = (EditText) findViewById(R.id.messageEditText);
        mSendButton = (Button) findViewById(R.id.sendButton);

        // Initialize message ListView and its adapter
        List<FriendlyMessage> friendlyMessages = new ArrayList<>();
        mMessageAdapter = new MessageAdapter(this, R.layout.item_message, friendlyMessages);
        mMessageListView.setAdapter(mMessageAdapter);

        // Initialize progress bar
        mProgressBar.setVisibility(ProgressBar.INVISIBLE);

        // ImagePickerButton shows an image picker to upload a image for a message
        mPhotoPickerButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // TODO: Fire an intent to show an image picker
            }
        });

        // Enable Send button when there's text to send
        mMessageEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (charSequence.toString().trim().length() > 0) {
                    mSendButton.setEnabled(true);
                } else {
                    mSendButton.setEnabled(false);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {
            }
        });
        mMessageEditText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(DEFAULT_MSG_LENGTH_LIMIT)});

        // Send button sends a message and clears the EditText
        mSendButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // TODO: Send messages on click

                // Clear input box
                mMessageEditText.setText("");
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        return super.onOptionsItemSelected(item);
    }
}

Here's the activity_main.xmlin @layout:

这是activity_main.xml@layout

<?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>

Here's the item_message.xmlin @layout:

这是item_message.xml@layout

<?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>

Here's the button_selector.xmlin @drawale:

这是button_selector.xml@drawale

<?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>

Here's the colors.xmlin @values:

这是colors.xml@values

<?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>

Here's the strings.xmlin @values:

这是strings.xml@values

<resources>
    <string name="app_name">Friendly Chat</string>
    <string name="sign_out">Sign Out</string>
    <string name="send_button_label">Send</string>
</resources>

Here's the styles.xmlin @values:

这是styles.xml@values

<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>

Here's the menu.xmlin @menu:

这是menu.xml@menu

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/sign_out_menu"
        android:title="@string/sign_out"
        app:showAsAction="never"/>
</menu>

Here's dimens.xmlin @values:

下面是dimens.xml@values

<resources>
    <!-- Default screen margins, per the Android Design guidelines. -->
    <dimen name="activity_horizontal_margin">16dp</dimen>
    <dimen name="activity_vertical_margin">16dp</dimen>
</resources>

Here's (w820dp) dimens.xmlin @values:

下面是(w820dp) dimens.xml@values

<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>

Please help me solve the error.

请帮我解决错误。

回答by ahmed ewess

the button selector is the cause of the problem .. if you read the remaining of the exception, the root cause will be mentioned .. as resource id can't be found !

按钮选择器是问题的原因.. 如果您阅读异常的其余部分,将提到根本原因.. 因为找不到资源 ID!

you have to change your button_selector.xml from:

你必须改变你的 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>

to:

到:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="#999999" android:state_enabled="false"/>
    <item android:drawable="@color/colorAccent" android:state_enabled="true"/>
</selector>

and it will work fine isA.

它会正常工作 isA。

回答by Qamar

Maybe this help anyone > I got similar crash

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

InflateException: Binary XML file line #0: Error inflating class EditText

InflateException: Binary XML file line #0: Error inflating class EditText

in Android 8.0 and 8.1 in release apk due to xml fontscause I didn't use latest support libs, I update support library version and it's fixed.

在 Android 8.0 和 8.1 发布 apk 中,由于xml 字体导致我没有使用最新的支持库,我更新了支持库版本并已修复。

xml having

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"/>

My gradle configs

我的 gradle 配置

BUILD_TOOLS_VER = '26.0.2'

TARGET_SDK_VER = 26
MIN_SDK_VER = 19

SUPPORT_LIBS_VER = '26.1.0' -> '27.0.2' (fix here)
GOOGLE_SERVICES_VER = '11.4.0' -> '11.8.0'

回答by Soumen

In my case the background xmls where not inside one drawable directory. I added all of them in drawable hdpi folder and then rebuild the project. That resolved this issue.

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

This worked for me!

这对我有用!

回答by Kim Hyun

Have you tried adding in gradle.propertiesfile this line below ?

您是否尝试在gradle.properties文件中添加以下这一行?

 android.enableAapt2=false

and then restarting the Gradle daemon command line by running

然后通过运行重新启动 Gradle 守护进程命令行

./gradlew --stop

https://developer.android.com/studio/releases/gradle-plugin.html#optimizations

https://developer.android.com/studio/releases/gradle-plugin.html#optimizations

回答by InsaneCat

I have got the same issue and I have resolved finally Opening the gradle.properties and added following line:

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

android.enableAapt2=false

Hope this helps you.

希望这对你有帮助。

回答by dilip yadav

May be you declare the drawable file into v21or v24folder to change this select the button_selector.xmlfile go to the show in menu option file select the cut button_selector.xmlfile and move into the drawable folder and paste it.

可能是您将可绘制文件声明到v21v24文件夹中以更改此选择button_selector.xml文件转到菜单选项文件中的显示选择剪切button_selector.xml文件并移动到可绘制文件夹并粘贴它。