Binary XML file line #0: Error inflating class Ima

2019-01-14 13:48发布

问题:

I am using Android studio 3.0
I am getting tired fixing this problem. the app does not execute it always show me this error Binary XML file line #0: Error inflating class ImageView

this is my main_activity.xml, I have changed my xml I used ConstraintLayout

  <?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 code for main activity

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);

  }
 }

this is the full log,

  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) 

回答1:

Some SVGs sources seem to not be fully supported. Add below line within your activity's onCreate() method:

AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);

and make sure you use below as attribute to your ImageView in your xml file: app:srcCompact instead of android:src to define your image.



回答2:

In Android Studio version 3.0.0 and above, once we try to add images to drawable folder, it will ask you to 'Choose Destination Directory'. At that time, choose drawable instead of drawable-v24.

Keep Coding........ :)



回答3:

don't paste your image in drawable(v-24), paste it into the drawable folder and clean the project and then run it will work.



回答4:

Solution for this kind of problem: Binary XML file line #0: Error inflating class ImageView

Solution:

  1. Check Your Image Folder, whether the image file is in Drawer or Drawer-24 or not. Example Here

Firebase image file is in Drawer-24

  1. If image file is in drawer-24, Right Click the File and select Refactor and then click Move

  2. Then remove the word "-24" and Click OK and RUN again.

Like this

DONE.



回答5:

Use android:src="@drawable/doctor" in your ImageView



回答6:

As far as I know, this problem occur when we paste image from explorer to drawable folder then it ask weather to copy drawable or drawable-v24 and if you have pasted images in both directory structures, or have one more images with different directory structure then it will show this error, make sure to copy image only in 'drawable' directory structure :)



回答7:

If you already have the option in the accepted answer check if the vector is in the in the "drawable-anydpi" folder. Moving it to the "drawable" folder fixes it.



回答8:

I'm using app:srcCompat instead of android:src. It works in android studio 3.0