我已经开发了一个应用程序一个星期了,一切都很顺利。对于我测试我开发的应用程序的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()调用上抛出异常的地方)。为了您的信息,在本课程中我只处理三个按钮,根据用户点击的内容,它会启动一个或多个活动。
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补丁源图像感到麻烦,我甚至都没有。我的所有drawable都是.xml或.png文件,但根本不是.9.png。这太奇怪了。
答案 0 :(得分:0)
好吧,人们似乎我遇到了一个我甚至没有的9补丁源图像的麻烦。我的所有drawable都是.xml。问题变得越来越神秘了。我甚至从.xml文件中删除了所有小部件,并且错误一直被抛出。不知道出了什么问题。