![](/img/trans.png)
[英]Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class android.widget.ImageView
[英]Caused by: android.view.InflateException Error inflating class in Android app
我已經開發了一個應用程序已有一個星期了,一切都很好。 在測試我開發的應用程序的3種設備中,我都沒有遇到任何麻煩。 但是現在我只是在這三台設備中的一台上運行了該應用程序,一切都陷入了困境。 最奇怪的是,該應用程序可以在其他兩個設備上完美運行,但是在第三個設備上,它在第一次onCreate()調用時崩潰。 我真的不知道我在做什么錯,因為我的設備之一無法正常工作。 這是我的代碼。
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。
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>
您可能會注意到,我對這些按鈕使用了自定義樣式。 樣式是:
<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錯誤。 由於時間很長,所以我只寫原因:
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中 onCreate()的setContentView()調用
如果您能花點時間檢查我的代碼,並讓我知道我做錯了什么,我將非常高興。 到目前為止,這是我很長一段時間以來遇到的最奇怪的麻煩。 提前致謝!
好吧,人們似乎對我甚至沒有的9補丁源圖像遇到了麻煩。 我所有的可繪制對象都是.xml或.png文件,但根本不是.9.png文件。 太奇怪了
好吧,人們似乎對我甚至沒有的9補丁源圖像遇到了麻煩。 我所有的可繪制對象都是.xml。 問題正在變得超現實。 我什至從.xml文件中刪除了所有小部件,並且錯誤不斷拋出。 不知道出了什么問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.