Reputation: 6839
I am trying to load a .png file for for things in my app that do not have images.
I have my noImage.png in the res>drawable directory and try and set it with:
//set image
ImageView im1 = (ImageView) findViewById(R.id.image);
if(e.largeLabel.equals("N/A")){
//set image as png
im1.setImageResource(R.drawable.noImage.png);
}
else{
ImageDownloadTask imageD = new ImageDownloadTask(im1);
imageD.execute(e.largeLabel);
}
I get the error:
png cannot be resolved or is not a field
Update:
Based on some answers below I changed my code to this:
im1.setImageResource((Integer) R.drawable.noImage);
But I get this force close error:
06-24 21:05:58.922: E/AndroidRuntime(20741): FATAL EXCEPTION: main
06-24 21:05:58.922: E/AndroidRuntime(20741): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.beerportfoliopro/com.example.beerportfoliopro.BeerPage}: java.lang.NullPointerException
06-24 21:05:58.922: E/AndroidRuntime(20741): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2332)
06-24 21:05:58.922: E/AndroidRuntime(20741): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2368)
06-24 21:05:58.922: E/AndroidRuntime(20741): at android.app.ActivityThread.access$600(ActivityThread.java:151)
06-24 21:05:58.922: E/AndroidRuntime(20741): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1330)
06-24 21:05:58.922: E/AndroidRuntime(20741): at android.os.Handler.dispatchMessage(Handler.java:99)
06-24 21:05:58.922: E/AndroidRuntime(20741): at android.os.Looper.loop(Looper.java:155)
06-24 21:05:58.922: E/AndroidRuntime(20741): at android.app.ActivityThread.main(ActivityThread.java:5536)
06-24 21:05:58.922: E/AndroidRuntime(20741): at java.lang.reflect.Method.invokeNative(Native Method)
06-24 21:05:58.922: E/AndroidRuntime(20741): at java.lang.reflect.Method.invoke(Method.java:511)
06-24 21:05:58.922: E/AndroidRuntime(20741): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1074)
06-24 21:05:58.922: E/AndroidRuntime(20741): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:841)
06-24 21:05:58.922: E/AndroidRuntime(20741): at dalvik.system.NativeStart.main(Native Method)
06-24 21:05:58.922: E/AndroidRuntime(20741): Caused by: java.lang.NullPointerException
06-24 21:05:58.922: E/AndroidRuntime(20741): at com.example.beerportfoliopro.BeerPage.onCreate(BeerPage.java:65)
06-24 21:05:58.922: E/AndroidRuntime(20741): at android.app.Activity.performCreate(Activity.java:5066)
06-24 21:05:58.922: E/AndroidRuntime(20741): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1102)
06-24 21:05:58.922: E/AndroidRuntime(20741): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2288)
06-24 21:05:58.922: E/AndroidRuntime(20741): ... 11 more
Upvotes: 2
Views: 918
Reputation: 6839
My problem was the name of my image. I can not have uppercase letters in the res folder. I had to change the name from noImage.png to noimage.png and everyone was correct in that I do not use the .png when calling it in android.
Upvotes: 2
Reputation: 5440
Try this,
im1.setImageDrawable(getResources().getDrawable(R.drawable.noImage));
Upvotes: 0
Reputation: 9125
You don't specify the file extension when accessing resources. Use R.drawable.noImage
instead.
Upvotes: 1