web-dev-qa-db-fra.com

android.view.InflateException: Ligne de fichier XML binaire # 0: Erreur lors du gonflage de la classe Button

Je suis un cours sur Audacity et j'ai décidé de taper tout le code, mais j'ai rencontré une erreur lorsque j'ai essayé de lancer l'application.

Je reçois l'erreur suivante:

        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                                                                     

Voici l'activité principale:

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);
    }
}

Voici le activity_main.xml dans @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>

Voici le item_message.xml dans @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>

Voici le button_selector.xml dans @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>

Voici le colors.xml dans @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>

Voici le strings.xml dans @values:

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

Voici le styles.xml dans @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>

Voici le menu.xml dans @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>

Voici dimens.xml dans @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>

Voici (w820dp) dimens.xml dans @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>

S'il vous plaît aidez-moi à résoudre l'erreur.

19
Yeezus

le sélecteur de bouton est la cause du problème .. si vous lisez le reste de l'exception, la cause première sera mentionnée .. car l'identifiant de ressource ne peut pas être trouvé!

vous devez changer votre fichier button_selector.xml de:

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

à:

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

et cela fonctionnera bien isA.

8
ahmed ewess

Dans mon cas, les xmls d’arrière-plan ne se trouvaient pas dans un seul répertoire. Je les ai tous ajoutés dans un dossier hdpi pouvant être dessiné, puis reconstruit le projet. Cela a résolu ce problème.

Cela a fonctionné pour moi!

6
Soumen

Avez-vous essayé d’ajouter gradle.properties le fichier ci-dessous?

 Android.enableAapt2=false

puis en redémarrant la ligne de commande du démon Gradle en exécutant

./gradlew --stop

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

1
Kim Hyun

Peut-être que cette aide aider quelqu'un> J'ai eu un crash similaire

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

in Android 8.0 et 8.1 dans les versions apk en raison de polices xml parce que je n'ai pas utilisé les dernières bibliothèques de support, je mets à jour la version de la bibliothèque de support et elle est corrigée.

xml ayant

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

Mes notes de configuration

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'
0
Qamar

J'ai le même problème et j'ai finalement résolu l'ouverture du fichier gradle.properties et ajouté la ligne suivante:

Android.enableAapt2=false

J'espère que cela vous aide.

0
InsaneCat