2

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
Mike
  • 6,751
  • 23
  • 75
  • 132
  • Why did you add the cast? – Jong Jun 25 '13 at 01:08
  • I get an error otherwise: The method setImageResource(int) in the type ImageView is not applicable for the arguments (Object) – Mike Jun 25 '13 at 01:09
  • wooooow. wait what? Object? what the hell have you done with the R file? R.drawable.(...) should return an integer. I think you have messed the imports. – Diogo Bento Jun 25 '13 at 01:17
  • @DiogoBento where can I find it so I can try and fix it? Eclipse suggested an edit to it earlier when I was trying to get my png to work... – Mike Jun 25 '13 at 01:20
  • look at the imports, the R file should be a reference to a class **inside** your project. not the android one neither anything else. You dont have to create an R file because it is generated automatically. – Diogo Bento Jun 25 '13 at 01:23
  • and btw, I truly recommend you to read some book about android.. – Diogo Bento Jun 25 '13 at 01:26
  • I know eclipse edited the r file when I said yes to one of its suggestions, how can I undo it? – Mike Jun 25 '13 at 01:27
  • http://stackoverflow.com/a/2757131/2211156 – Diogo Bento Jun 25 '13 at 01:29
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/32300/discussion-between-mike-and-diogo-bento) – Mike Jun 25 '13 at 01:30
  • In Netbeans all I need to do is "clean and build" to regenerate the R file, I believe there is something similar in Eclipse, and you should really never manually modify R file – Chor Wai Chun Jun 25 '13 at 02:00
  • What's the code at the line giving the NPE (line 65)? – Ginger McMurray Jun 25 '13 at 02:10

5 Answers5

2

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.

Mike
  • 6,751
  • 23
  • 75
  • 132
  • this should be the accepted answer, we cannot have any unique character for the resource naming, allowed: `lowercase & underscore` cmiiw – mochadwi Feb 03 '20 at 01:15
1

You don't specify the file extension when accessing resources. Use R.drawable.noImage instead.

Jong
  • 9,045
  • 3
  • 34
  • 66
0

try without end : im1.setImageResource(R.drawable.noImage);

Adrien Cerdan
  • 1,005
  • 1
  • 11
  • 21
  • Then I get this error: The method setImageResource(int) in the type ImageView is not applicable for the arguments (Object) – Mike Jun 25 '13 at 01:04
0

use this im1.setImageResource(R.drawable.noImage);

Vijay Rajput
  • 1,091
  • 1
  • 13
  • 18
0

Try this,

im1.setImageDrawable(getResources().getDrawable(R.drawable.noImage));
Basim Sherif
  • 5,384
  • 7
  • 48
  • 90