应用程序在启动时崩溃-android.view.InflateException:二进制XML文件行#0:二进制XML
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了应用程序在启动时崩溃-android.view.InflateException:二进制XML文件行#0:二进制XML相关的知识,希望对你有一定的参考价值。
我是android的新手,我创建了一个非常简单的应用程序,通过构建过程没有问题但是当我尝试启动时(无论是在模拟器上还是在我的Android设备中)它崩溃(图像吼叫)。我不确定如何解决这个问题,我检查了logcat,我得到了一些消息,我认为可能指向错误来源,但不知道它们是什么意思。应用程序在我的设备One Plus 3T(android 8.0.0)上的模拟器(PIXEL 2 XL API 27)上崩溃。
Bellow my xml,java代码和日志结果
我的xml布局:
<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:background="#de7905">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/texto_frase_dia"
android:id="@+id/botaoNovaFraseId"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textoNovaFraseId"
android:layout_below="@+id/botaoNovaFraseId"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="20dp"
android:layout_alignRight="@+id/botaoNovaFraseId"
android:layout_alignEnd="@+id/botaoNovaFraseId"
android:textSize="@dimen/texto_nova_frase"
android:textColor="#fbfff7" />
我的java代码:
package ie.app.frasedodia;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
private TextView textoNovaFrase;
private Button botaoNovaFrase;
private String[] frases = {
"oi",
"tudo bem?",
"ddt?"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textoNovaFrase = (TextView) findViewById(R.id.textoNovaFraseId);
botaoNovaFrase = (Button) findViewById(R.id.botaoNovaFraseId);
botaoNovaFrase.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Random randomico = new Random();
int numeroAleatorio = randomico.nextInt( frases.length );
textoNovaFrase.setText( frases[ numeroAleatorio ] );
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
我的日志输出:
03-03 15:46:24.740 11858-11858/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: ie.app.frasedodia, PID: 11858
java.lang.RuntimeException: Unable to start activity ComponentInfo{ie.app.frasedodia/ie.app.frasedodia.MainActivity}: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class TextView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class TextView
Caused by: android.view.InflateException: Binary XML file line #0: Error inflating class TextView
Caused by: java.lang.UnsupportedOperationException: Can't convert value at index 2 to dimension: type=0x4
at android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:730)
at android.widget.TextView.<init>(TextView.java:1226)
at android.widget.TextView.<init>(TextView.java:820)
at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:75)
at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:71)
at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:103)
at android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1024)
at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1081)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:772)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at ie.app.frasedodia.MainActivity.onCreate(MainActivity.java:27)
at android.app.Activity.performCreate(Activity.java:6999)
at android.app.Activity.performCreate(Activity.java:6990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
我的gradle设置:
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
defaultConfig {
applicationId "ie.app.frasedodia"
minSdkVersion 23
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}
答案
这个
Caused by: java.lang.UnsupportedOperationException: Can't convert value at index 2 to dimension: type=0x4
at android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:730)
让我认为你在@dimen/texto_nova_frase
有错误的单位。建议的单位是sp。
以上是关于应用程序在启动时崩溃-android.view.InflateException:二进制XML文件行#0:二进制XML的主要内容,如果未能解决你的问题,请参考以下文章