2

App crashes whenever I try to add an image in Android Studio 3. Please Help.

LOGCAT:

12-02 01:13:14.289 10530-10530/? E/AndroidRuntime: FATAL EXCEPTION: main Process: com.divyam.demo2, PID: 10530 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.divyam.demo2/com.divyam.demo2.MainActivity}: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class ImageView at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) at android.app.ActivityThread.-wrap11(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) Caused by: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class ImageView at android.view.LayoutInflater.inflate(LayoutInflater.java:539) 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 com.divyam.demo2.MainActivity.onCreate(MainActivity.java:16) at android.app.Activity.performCreate(Activity.java:6237) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) at android.app.ActivityThread.-wrap11(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) Caused by: android.view.InflateException: Binary XML file line #0: Error inflating class ImageView at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:782) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) at android.view.LayoutInflater.rInflate(LayoutInflater.java:835) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) 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 com.divyam.demo2.MainActivity.onCreate(MainActivity.java:16) at android.app.Activity.performCreate(Activity.java:6237) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) at android.app.ActivityThread.-wrap11(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f060054 at android.content.res.Resources.getValue(Resources.java:1351) at android.support.v7.widget.AppCompatDrawableManager.loadDrawableFromDelegates(AppCompatDrawableManager.java:330) at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:195) at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:188) at android.support.v7.content.res.AppCompatResources.getDrawable(AppCompatResources.java:100) at android.support.v7.widget.AppCompatImageHelper.loadFromAttributes(AppCompatImageHelper.java:58) at android.support.v7.widget.AppCompatImageView.(AppCompatImageView.java:78) at android.support.v7.widget.AppCompatImageView.(AppCompatImageView.java:68) at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:106) 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:746) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) at android.view.LayoutInflater.rInflate(LayoutInflater.java:835) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) 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 com.divyam.demo2.MainActivity.onCreate(MainActivity.java:16) at android.app.Activity.performCreate(Activity.java:6237) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) at android.app.ActivityThread.-wrap11(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) }

activity_main.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.divyam.demo2.MainActivity"
    tools:layout_editor_absoluteY="81dp">

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="344dp"
        android:text="Button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="18dp"
        app:layout_constraintBottom_toBottomOf="@+id/button2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:srcCompat="@drawable/download" />

</android.support.constraint.ConstraintLayout>

Main Activity.java

    public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }}

When I delete the ImageView the app works just fine. I have tried everything resizing the image and reinstalling android studio.

Can anyone figure out the problem?

3
  • Something's wrong with the drawable- it can't find it. Is it a valid drawable file? Commented Dec 1, 2017 at 20:09
  • check my answer below Commented Dec 1, 2017 at 20:10
  • What happens when u add some other .jpg image? Commented Apr 6, 2018 at 14:34

6 Answers 6

1

Image resolution/dimension is the problem. Just check and change the resolution/dimension of the image file to a suitable size for mobile phones. I faced the same problem.

Sign up to request clarification or add additional context in comments.

1 Comment

I did this tutorial: developer.android.com/codelabs/… and it keeps crashing. I checked multiple things. Then, read your post, and yes, the picture file is too large!
0

Change this :

  <ImageView
        ...
        app:srcCompat="@drawable/download" />

To this:

<ImageView
        ...
        android:src="@drawable/download" />

UPDATE

Check the drawable directory, probably your drawable is in other folders like drawable-large , drawable-xxhdpi , etc. then clean and rebuild your project

5 Comments

could you put your download.png file inside the /drawable/ folder?
is a png file or xml file ? if it's xml put the content
It is a jpg file
check the directory, probably your drawable is in other folders like drawable-large , drawable-xxhdpi , etc. then clean and rebuild your project.
Yes thank you. My photos were getting into drawable-v24. Now after changing it works fine.
0

You may messed up with difference between "@+id" and "@id". Try to correct this line of that ImageView in your activity_main.xml

app:layout_constraintBottom_toBottomOf="@+id/button2"

into

app:layout_constraintBottom_toBottomOf="@id/button2"

and retry.

2 Comments

What is the file type of image download you are using?
check my answer please.
0

Use PNG or SVG image as the source of your imageView. And change the below line:

app:srcCompat="@drawable/download"

To:

android:src="@drawable/download"

4 Comments

I used a png file and still the app is crashing
Did you try clean and building the project?
Thank you. My problem got resolved, images were going to drawable-v24 folder instead of drawable.
Yeah, I see. Glad you got help. :)
0

Make sure your image name has no spaces in between and starts with a small case letter.in additon save it in drawable folder instead of drawable(24) otherwise it reports error 90% of the time.

Comments

-1

Essentially you need to do a trial and eror by following below mentioned steps:

  • Try New Project and add you Image first or whatever it may be and then run the emulator.
  • If it is working, then start adding textView, buttons and other required objects to your activity and run your emulator at every instance you are adding items.
  • If everything works fine, add code to your Main activity from old project and add all the changes.

It should Run fine. I actually tried this for one of my projects where I was facing a similar issue and it worked.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.