1

I'm kinda new to android development and having problem showing images from my drawable resource file with java code. I simplified my original app to isolate the problem, so now it is just a blank activity with one image view in the center.

xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<ImageView
    android:id="@+id/imageView000"
    android:layout_width="82dp"
    android:layout_height="82dp"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true" />

public class MainActivity extends AppCompatActivity {

public ImageView images;

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

@Override
protected void onStart() {
    super.onStart();
    images = findViewById(R.id.imageView000);
    images.setImageResource(R.drawable.imageNameInDrawableFile);

}

}

There is no error in compilation. But when I run this on the virtual device or my phone, the app crashes on start. What's wrong?

Update: thank for the tips everyone. the problem was the directory of the image, it was "E:\AndroidStudioProjects\TestApplication\app\src\main\res\drawable-v24", just removed the "-v24" in directory shown after i drop myImage into drawable folder and it worked perfectly after.

2
  • 2
    the app crashes on start. Whats wrong? Why don't you share crash log with question Commented Jul 9, 2019 at 10:59
  • 1
    it just doesn't open. "app stopped working" message. Commented Jul 9, 2019 at 11:12

2 Answers 2

1

Use images.setImageResource(R.drawable.imageNameInDrawableFile);

setImageDrawable() requires a Drawable to be passed but you are passing an integer.

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

Comments

0

Just use you're code in onCreate.

Cause onStart includes when the application is first created and when it is brought back on the screen without being terminated.

public ImageView images;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    images = findViewById(R.id.imageView000);
    images.setImageDrawable(ContextCompat.getDrawable(activity, R.drawable.your_image));
    //or
    images.setImageResource(R.drawable.your_image);
}

7 Comments

I moved those codes to onCreate, its the same: "Unfortunately, app has stopped."
Context activity = null; images.setImageDrawable(ContextCompat.getDrawable(activity, R.drawable.imageName)); //didnt work
images.setImageResource(R.drawable.your_image); // didnt work.
@Datis you have to pass you're drawable make sure you have drawable present in drawable folder.
i found the problem, thanks for the tip mate. it was the directory of the image when i place it in drawable folder, "E:\AndroidStudioProjects\TestApplication\app\src\main\res\drawable-v24". just had to delete "-v24"... i was struggeling for 2 day!!!
|

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.