问题描述
我遇到了一个奇怪的问题.每当在Activity
中添加EditText
时,我的Android应用程序就会崩溃.为了进行测试,我打开了一个新的干净项目,如果还有EditText
,新项目也会崩溃.因此,我认为问题是由Android Studio或其他环境设置引起的.
I encountered a strange problem. My Android app would crash whenever I add an EditText
in an Activity
. To test, I open a new clean project, and the new project will crash if there is an EditText
as well. Therefore, I think the problem is caused from Android Studio or other environment setting.
以下是activity_main.xml
Following is the activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
android:id="@+id/activity_main"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="yichun.myapplication.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="@+id/textView"
app:layout_constraintLeft_toLeftOf="@+id/activity_main"
app:layout_constraintTop_toTopOf="@+id/activity_main"
app:layout_constraintRight_toRightOf="@+id/activity_main"
app:layout_constraintBottom_toBottomOf="@+id/activity_main"/>
<EditText
android:layout_width="218dp"
android:layout_height="45dp"
android:inputType="textPersonName"
android:text="Name"
android:ems="10"
tools:layout_editor_absoluteX="90dp"
android:id="@+id/editText"
app:layout_constraintBottom_toTopOf="@+id/textView" android:layout_marginBottom="40dp"/>
</android.support.constraint.ConstraintLayout>
MainActivity.java 公共类MainActivity扩展了AppCompatActivity {
MainActivity.java public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
AndroidManifest.xml
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
错误消息
E/AndroidRuntime: FATAL EXCEPTION: main
Process: yichun.myapplication, PID: 3239
java.lang.RuntimeException: Unable to start activity ComponentInfo{yichun.myapplication/yichun.myapplication.MainActivity}: android.view.InflateException: Binary XML file line #20: Error inflating class EditText
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: android.view.InflateException: Binary XML file line #20: Error inflating class EditText
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at yichun.myapplication.MainActivity.onCreate(MainActivity.java:11)
at android.app.Activity.performCreate(Activity.java:5937)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: android.content.res.Resources$NotFoundException: File res/drawable-v21/abc_edit_text_material.xml from drawable resource ID #0x7f020015
at android.content.res.Resources.loadDrawableForCookie(Resources.java:2428)
at android.content.res.Resources.loadDrawable(Resources.java:2330)
at android.content.res.TypedArray.getDrawable(TypedArray.java:749)
at android.view.View.<init>(View.java:3730)
at android.widget.TextView.<init>(TextView.java:634)
at android.widget.EditText.<init>(EditText.java:65)
at android.widget.EditText.<init>(EditText.java:61)
at android.support.v7.widget.AppCompatEditText.<init>(AppCompatEditText.java:60)
at android.support.v7.widget.AppCompatEditText.<init>(AppCompatEditText.java:56)
at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:112)
at android.support.v7.app.AppCompatDelegateImplV7.createView(AppCompatDelegateImplV7.java:980)
at android.support.v7.app.AppCompatDelegateImplV7.onCreateView(AppCompatDelegateImplV7.java:1039)
at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:725)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at yichun.myapplication.MainActivity.onCreate(MainActivity.java:11)
at android.app.Activity.performCreate(Activity.java:5937)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #24: <nine-patch> requires a valid 9-patch source image
at android.graphics.drawable.NinePatchDrawable.updateStateFromTypedArray(NinePatchDrawable.java:445)
at android.graphics.drawable.NinePatchDrawable.inflate(NinePatchDrawable.java:401)
at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:1095)
at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:185)
at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:1095)
at android.graphics.drawable.InsetDrawable.inflate(InsetDrawable.java:104)
at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:1095)
at android.graphics.drawable.Drawable.createFromXml(Drawable.java:1017)
at android.content.res.Resources.loadDrawableForCookie(Resources.java:2418)
at android.content.res.Resources.loadDrawable(Resources.java:2330)
at android.content.res.TypedArray.getDrawable(TypedArray.java:749)
at android.view.View.<init>(View.java:3730)
at android.widget.TextView.<init>(TextView.java:634)
at android.widget.EditText.<init>(EditText.java:65)
at android.widget.EditText.<init>(EditText.java:61)
at android.support.v7.widget.AppCompatEditText.<init>(AppCompatEditText.java:60)
at android.support.v7.widget.AppCompatEditText.<init>(AppCompatEditText.java:56)
at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:112)
at android.support.v7.app.AppCompatDelegateImplV7.createView(AppCompatDelegateImplV7.java:980)
at android.support.v7.app.AppCompatDelegateImplV7.onCreateView(AppCompatDelegateImplV7.java:1039)
at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:725)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at yichun.myapplication.MainActivity.onCreate(MainActivity.java:11)
at android.app.Activity.performCreate(Activity.java:5937)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
推荐答案
使用gradle插件的2.2.x版本可能会导致此问题.
Using 2.2.x version of the gradle plugin could cause this.
降级可能值得一试:
// project level build.gradle
dependencies {
classpath 'com.android.tools.build:gradle:2.1.0'
}
我想它最终会被修复,所以未来的读者可以尝试升级而不是降级:)
I suppose it will be fixed eventually, so future readers can try upgrading instead of downgrading :)
这篇关于Android错误膨胀类EditText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!