【发布时间】:2016-04-05 01:02:03
【问题描述】:
我已经开发一个应用程序一周了,一切都很顺利。在我测试我开发的应用程序的 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。
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 错误。由于篇幅较长,我只写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中onCreate()的setContentView()调用是什么
如果您能花时间检查我的代码并告诉我是否做错了什么,我将非常高兴。到目前为止,这是我长期以来面临的最奇怪的麻烦。提前致谢!
好吧,人们似乎在使用 9 补丁源图像时遇到了问题,而我什至没有。我所有的可绘制对象都是 .xml 或 .png 文件,但根本不是 .9.png。这太奇怪了。
【问题讨论】:
-
显然您的一个可绘制对象不是正确的 9-patch 图像:“需要有效的 9-patch 源图像”
-
我注意到了,但这是什么意思?什么是 9-patch 源图像?
-
9 补丁图像是可扩展的图像。您可以在此处找到一些文档:developer.android.com/intl/es/tools/help/draw9patch.html 在您的可绘制对象中查找扩展名为“.9.png”的图像
-
感谢您的回答!无论如何,这很奇怪,因为我从 .xml 中删除了任何可绘制调用,并且错误不断被抛出。
-
而且我也没有任何带有扩展名“.9.png”的可绘制对象,或者至少我没有看到这个扩展名
标签: java android inflate-exception