Miguel Duran Diaz
Miguel Duran Diaz

Reputation: 312

App crashes when trying to use setImageResource

I was building another larger app and at some point I needed to dynamically change images and my app crashed. So, to test functionality I made another really simple app that just displays a picture with code:

package com.example.imagetest;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ImageView imageView = (ImageView) findViewById(R.id.imageview);
        imageView.setImageResource(R.drawable.pugs);
    }
}

The error reported by Android Studio is: 03-07 11:30:01.146 20758-20758/? E/Trace: error opening trace file: No such file or directory (2) 03-07 11:30:01.284 20758-20758/com.example.imagetest E/dalvikvm: Could not find class 'android.support.v4.view.ViewCompat$OnUnhandledKeyEventListenerWrapper', referenced from method android.support.v4.view.ViewCompat.addOnUnhandledKeyEventListener 03-07 11:30:01.286 20758-20758/com.example.imagetest E/dalvikvm: Could not find class 'android.view.WindowInsets', referenced from method android.support.v4.view.ViewCompat.dispatchApplyWindowInsets 03-07 11:30:01.298 20758-20758/com.example.imagetest E/dalvikvm: Could not find class 'android.view.WindowInsets', referenced from method android.support.v4.view.ViewCompat.onApplyWindowInsets 03-07 11:30:01.300 20758-20758/com.example.imagetest E/dalvikvm: Could not find class 'android.view.View$OnUnhandledKeyEventListener', referenced from method android.support.v4.view.ViewCompat.removeOnUnhandledKeyEventListener 03-07 11:30:01.306 20758-20758/com.example.imagetest E/dalvikvm: Could not find class 'android.support.v4.view.ViewCompat$1', referenced from method android.support.v4.view.ViewCompat.setOnApplyWindowInsetsListener 03-07 11:30:01.354 20758-20758/com.example.imagetest E/dalvikvm: Could not find class 'android.graphics.drawable.RippleDrawable', referenced from method android.support.v7.widget.AppCompatImageHelper.hasOverlappingRendering 03-07 11:30:01.396 20758-20758/com.example.imagetest E/AndroidRuntime: FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.imagetest/com.example.imagetest.MainActivity}: android.content.res.Resources$NotFoundException: Resource ID

0x7f060063

and a lot of other stuff. Here's the weird thing: I changed the picture to another folder "mipmap" (before it was in drawable: res/drawable) and the app doesn't crash! But it doesn't show the image either, just nothing on screen. the XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".MainActivity">

    <ImageView
        android:id="@+id/imageview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

Not sure if the image is important, but it was a really cute picture of 3 pugs(Resolution approx 4k) and I added it to drawable just with copy/paste.

Upvotes: 1

Views: 391

Answers (2)

ismail alaoui
ismail alaoui

Reputation: 6073

Your problem is the wrong placement of resources(images). You are using a device with API below 24 , so resource for API 23 and below is null, because they only API 24 and up.

please put your pugs picture in drawable folder not drawable-v24 , and also don't use a huge image!

Upvotes: 1

Wesely
Wesely

Reputation: 1475

    imageView.setImageResource(R.drawable.pugs);

You need specify which R it is in the import part.

example:

    import com.sample.me.mydraft.R

Upvotes: 0

Related Questions