Caused by: android.view.InflateException Error inflating class in Android app

怎甘沉沦 提交于 2019-12-12 03:26:49

问题


I've been developing an app for a week now and everything was going fine. I wasn't having any troubles with any of the 3 devices in which I test the Apps I develope. But now I just runned the app on one of these three devices and everything went to hell. The weirdest thing is that the app works perfectly on the other two devices but on the third one it crashes on the first onCreate() call. I sincerely don't know what am I doing wrong that it's not working on one of my devices. Here's my code.

Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="myapp.miquel.mqlapps.hombresmujeresapp">

    <meta-data
        android:name="ADMOB_PUBLISHER_ID"
        android:value="here_goes_my_real_admob_id" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="ndroid.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

    <application
        android:allowBackup="true"
        android:exported="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:theme="@style/AppTheme">

        <!--
             ATTENTION: This was auto-generated to add Google Play services to your project for
             App Indexing.  See https://g.co/AppIndexing/AndroidStudio for more information.
        -->
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

        <activity
            android:name=".TestActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait">
        </activity>

        <activity
            android:name=".ResultActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait">
        </activity>

        <activity
            android:name=".MenuActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>


        <activity
            android:name=".SettingsActivity"
            android:label="@string/title_activity_settings"
            android:screenOrientation="portrait" />
        <activity
            android:name=".GenderChoiceActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait">

        </activity>
        <activity
            android:name=".AboutAppActivity"
            android:label="@string/title_activity_about_app"
            android:screenOrientation="portrait" >
        </activity>
        <activity
            android:name=".MQLAppsIniActivity"
            android:label="@string/app_name">
        </activity>
    </application>

</manifest>

MenuActivity.java (here is where the exception is being thrown on the setContentView() call inside the onCreate() method). For your information, in this class I just handle three buttons, and depending on what the user clicks, it starts one Activity or another.

package myapp.miquel.mqlapps.hombresmujeresapp;

import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

public class MenuActivity extends AppCompatActivity {

    public static String gender = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu);

        Typeface tf_existence = Typeface.createFromAsset(MenuActivity.this.getApplicationContext().getAssets(), "Existence-Light.otf");

        Button initTest = (Button) findViewById(R.id.initTest);
        initTest.setTypeface(tf_existence);

        Button ajustes = (Button) findViewById(R.id.ajustes);
        ajustes.setTypeface(tf_existence);

        Button sobreLaApp = (Button) findViewById(R.id.sobreApp);
        sobreLaApp.setTypeface(tf_existence);



        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            gender = extras.getString("GENERO");
        }



        initTest.setOnClickListener(new View.OnClickListener() {
            @Override

            public void onClick(final View v) {

                MenuActivity.this.finish();
                Intent intent = new Intent(MenuActivity.this, TestActivity.class);
                intent.putExtra("GENERO", gender);
                startActivity(intent);
            }
        });

        ajustes.setOnClickListener(new View.OnClickListener() {
            @Override

            public void onClick(final View v) {

                //MenuActivity.this.finish();
                Intent intent = new Intent(MenuActivity.this, SettingsActivity.class);
                intent.putExtra("GENERO", gender);
                startActivity(intent);
            }
        });

        sobreLaApp.setOnClickListener(new View.OnClickListener() {
            @Override

            public void onClick(final View v) {

                //MenuActivity.this.finish();
                Intent intent = new Intent(MenuActivity.this, AboutAppActivity.class);
                startActivity(intent);
            }
        });

    }

    @Override
    public void onResume(){
        super.onResume();
        // put your code here...


    }

}

activity_menu.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="myapp.miquel.mqlapps.hombresmujeresapp.ResultActivity"
    android:background="#bbdefb">


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">

        <Button
            style="@style/ButtonInitTest"
            android:layout_width="wrap_content"
            android:layout_height="75dp"
            android:text="Iniciar test"
            android:id="@+id/initTest"
            android:layout_marginTop="45dp"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true" />

        <Button
            style="@style/ButtonSettings"
            android:layout_width="wrap_content"
            android:layout_height="75dp"
            android:text="Ajustes"
            android:id="@+id/ajustes"
            android:layout_below="@+id/initTest"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginTop="62dp"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true" />

        <Button
            style="@style/ButtonAbout"
            android:layout_width="wrap_content"
            android:layout_height="75dp"
            android:text="Sobre la App"
            android:id="@+id/sobreApp"
            android:layout_below="@+id/ajustes"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginTop="62dp"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true" />
    </RelativeLayout>
</RelativeLayout>

As you may notice, I'm using a custom style for those buttons. The style is:

<style name="ButtonNormalText" parent="@android:style/Widget.Button">
        <item name="android:textColor" >@color/black</item>
        <item name="android:textSize" >25dip</item>
        <item name="android:height" >44dip</item>
        <item name="android:background" >@drawable/default_button</item>
        <item name="android:focusable" >true</item>
        <item name="android:clickable" >true</item>
    </style>

<style name="ButtonInitTest" parent="ButtonNormalText">
        <item name="android:drawableLeft" >@drawable/test_icon</item>
    </style>

    <style name="ButtonSettings" parent="ButtonNormalText">
        <item name="android:drawableLeft" >@drawable/ic_settings</item>
    </style>

    <style name="ButtonAbout" parent="ButtonNormalText">
        <item name="android:drawableLeft" >@drawable/ic_about</item>
    </style>

And finally this is the LogCat error. Since it's pretty long I'll write just the Caused by:

FATAL EXCEPTION: main
Process: myapp.miquel.mqlapps.hombresmujeresapp, PID: 13239
   java.lang.RuntimeException: Unable to start activity ComponentInfo{myapp.miquel.mqlapps.hombresmujeresapp/myapp.miquel.mqlapps.hombresmujeresapp.MenuActivity}: android.view.InflateException: Binary XML file line #43: Error inflating class android.support.v7.internal.widget.ActionBarContextView

Caused by: android.view.InflateException: Binary XML file line #43: Error inflating class android.support.v7.internal.widget.ActionBarContextView

Caused by: java.lang.reflect.InvocationTargetException

Caused by: java.lang.RuntimeException: org.xmlpull.v1.XmlPullParserException: <internal>: <nine-patch> requires a valid 9-patch source image

Everything is happening at:

at myapp.miquel.mqlapps.hombresmujeresapp.MenuActivity.onCreate(MenuActivity.java:30)

Which is the setContentView() call from the onCreate() in MenuActivity

I'd be very pleased if you could please take the time to check my code and let me know if I'm doing something wrong. This is so far the weirdest trouble I've been facing for a long time. Thanks in advance!

Well, people it seems that I'm having troubles with a 9-patch source image that I don't even have. All of my drawables are .xml or .png files but not .9.png at all. This is so weird.


回答1:


Well, people it seems that I'm having troubles with a 9-patch source image that I don't even have. All of my drawables are .xml. The problem is getting surreal. I've even removed all of the widgets from the .xml file and the error keeps being thrown. Don't know what's going wrong.



来源:https://stackoverflow.com/questions/36415398/caused-by-android-view-inflateexception-error-inflating-class-in-android-app

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