当前位置: 首页 > 面试题库 >

二进制XML文件第0行:膨胀类ImageView时出错

孟英锐
2023-03-14
问题内容

我使用的是Android Studio 3.0,
我已经很累解决此问题。应用程序不执行它总是向我显示此错误二进制XML文件第0行:膨胀类ImageView的错误

这是我的main_activity.xml,我更改了使用ConstraintLayout的xml

  <?xml version="1.0" encoding="utf-8"?>
 <android.support.constraint.ConstraintLayout
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="com.example.razoo.mywages.MainActivity">

<ImageView
    android:id="@+id/imageView2"
    android:layout_width="wrap_content"
    android:layout_height="517dp"
    android:contentDescription="@string/button3"
    android:scaleType="fitXY"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:src="@drawable/doctor" />

<Button
    android:id="@+id/step1btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="4dp"
    android:background="@color/colorPrimary"
    android:onClick="nextPage"
    android:text="@string/button3"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.498"
    tools:layout_alignParentEnd="true"
    tools:layout_alignParentRight="true"
    tools:layout_alignParentTop="true"
    />

<Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    android:background="@color/colorPrimary"
    android:text="@string/button3"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    tools:layout_alignParentEnd="true"
    tools:layout_alignParentRight="true"
    tools:layout_alignParentTop="true" />

主要活动的Java代码

public class MainActivity extends Activity {
Button btn1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);//the error point here
    btn1 = (Button) findViewById(R.id.step1btn);


}
public void nextPage(View view){
    Intent StartNewActivity = new Intent(MainActivity.this, Main2Activity.class);
    startActivity(StartNewActivity);

  }
 }

这是完整的日志,

  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.razoo.mywages/com.example.razoo.mywages.MainActivity}: android.view.InflateException: Binary XML file line #0: Error inflating class android.widget.ImageView
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2365)
                  at android.app.ActivityThread.access$800(ActivityThread.java:148)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:135)
                  at android.app.ActivityThread.main(ActivityThread.java:5272)
                  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:909)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704)
               Caused by: android.view.InflateException: Binary XML file line #0: Error inflating class android.widget.ImageView
                  at android.view.LayoutInflater.createView(LayoutInflater.java:633)
                  at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55)
                  at android.view.LayoutInflater.onCreateView(LayoutInflater.java:682)
                  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741)
                  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 com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:387)
                  at android.app.Activity.setContentView(Activity.java:2188)
                  at com.example.razoo.mywages.MainActivity.onCreate(MainActivity.java:16)
                  at android.app.Activity.performCreate(Activity.java:5977)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2258)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2365) 
                  at android.app.ActivityThread.access$800(ActivityThread.java:148) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283) 
                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                  at android.os.Looper.loop(Looper.java:135) 
                  at android.app.ActivityThread.main(ActivityThread.java:5272) 
                  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:909) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704) 
               Caused by: java.lang.reflect.InvocationTargetException
                  at java.lang.reflect.Constructor.newInstance(Native Method)
                  at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
                  at android.view.LayoutInflater.createView(LayoutInflater.java:607)
                  at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55) 
                  at android.view.LayoutInflater.onCreateView(LayoutInflater.java:682) 
                  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741) 
                  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 com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:387) 
                  at android.app.Activity.setContentView(Activity.java:2188) 
                  at com.example.razoo.mywages.MainActivity.onCreate(MainActivity.java:16) 
                  at android.app.Activity.performCreate(Activity.java:5977) 
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) 
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2258) 
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2365) 
                  at android.app.ActivityThread.access$800(ActivityThread.java:148) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283) 
                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                  at android.os.Looper.loop(Looper.java:135) 
                  at android.app.ActivityThread.main(ActivityThread.java:5272) 
                  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:909) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704) 
               Caused by: android.content.res.Resources$NotFoundException: Resource "com.example.razoo.mywages:drawable/doctor" (7f060054)  is not a Drawable (color or path): TypedValue{t=0x1/d=0x7f060054 a=-1 r=0x7f060054}
                  at android.content.res.Resources.loadDrawableForCookie(Resources.java:2431)
                  at android.content.res.Resources.loadDrawable(Resources.java:2371)
                  at android.content.res.TypedArray.getDrawable(TypedArray.java:749)
                  at android.widget.ImageView.<init>(ImageView.java:146)
                  at android.widget.ImageView.<init>(ImageView.java:135)
                  at android.widget.ImageView.<init>(ImageView.java:131)
                  at java.lang.reflect.Constructor.newInstance(Native Method) 
                  at java.lang.reflect.Constructor.newInstance(Constructor.java:288) 
                  at android.view.LayoutInflater.createView(LayoutInflater.java:607) 
                  at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55) 
                  at android.view.LayoutInflater.onCreateView(LayoutInflater.java:682) 
                  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741) 
                  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 com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:387) 
                  at android.app.Activity.setContentView(Activity.java:2188) 
                  at com.example.razoo.mywages.MainActivity.onCreate(MainActivity.java:16) 
                  at android.app.Activity.performCreate(Activity.java:5977) 
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) 
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2258) 
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2365) 
                  at android.app.ActivityThread.access$800(ActivityThread.java:148) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283) 
                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                  at android.os.Looper.loop(Looper.java:135) 
                  at android.app.ActivityThread.main(ActivityThread.java:5272) 
                  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:909) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704)

问题答案:

似乎不完全支持某些SVG源。在活动的onCreate()方法中添加以下行:

AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);

并确保您在XML文件中使用以下属性作为ImageView的属性:app:srcCompact而不是android:src来定义图像。



 类似资料: