原因:android.view.InflateException膨胀Android应用程序中的类时出错 [英] Caused by: android.view.InflateException Error inflating class in Android app

查看:109
本文介绍了原因:android.view.InflateException膨胀Android应用程序中的类时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开发了一个应用程序已有一个星期了,一切都进行得很好.在测试我开发的应用程序的3种设备中,我都没有遇到任何麻烦.但是现在我只是在这三台设备中的一台上运行了该应用程序,一切都陷入了困境.最奇怪的是,该应用程序可以在其他两个设备上完美运行,但是在第三个设备上,它在第一个onCreate()调用时崩溃.我真的不知道我在做什么错,因为我的设备之一无法正常工作.这是我的代码.

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 (在onCreate()方法内的setContentView()调用中在这里抛出异常的位置).供您参考,在本课程中,我仅处理三个按钮,并且根据用户单击的内容,它会启动一个Activity或另一个Activity.

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>

最后这是LogCat错误.由于时间很长,所以我只写原因:

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

一切都在发生:

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

哪个是 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!

好吧,人们似乎在使用甚至没有的9补丁源图像时遇到了麻烦.我所有的可绘制对象都是.xml或.png文件,但根本不是.9.png文件.太奇怪了.

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.

推荐答案

好吧,人们似乎在使用甚至没有的9补丁源图像时遇到了麻烦.我所有的可绘制对象都是.xml.问题正在变得超现实.我什至从.xml文件中删除了所有小部件,并且错误不断抛出.不知道出了什么问题.

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.

这篇关于原因:android.view.InflateException膨胀Android应用程序中的类时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆