android imageView不喜欢我的.png文件

时间:2013-06-25 01:00:35

标签: android android-imageview

我正在尝试为我的应用中没有图片的内容加载.png文件。

我在res> drawable目录中有我的noImage.png并尝试设置它:

//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);
        }

我收到错误:

png cannot be resolved or is not a field

更新

根据以下一些答案,我将代码更改为:

im1.setImageResource((Integer) R.drawable.noImage);

但我得到了这种力量关闭错误:

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

5 个答案:

答案 0 :(得分:1)

访问资源时不指定文件扩展名。请改用R.drawable.noImage

答案 1 :(得分:0)

无尽的尝试:im1.setImageResource(R.drawable.noImage);

答案 2 :(得分:0)

使用此im1.setImageResource(R.drawable.noImage);

答案 3 :(得分:0)

试试这个,

im1.setImageDrawable(getResources().getDrawable(R.drawable.noImage));

答案 4 :(得分:0)

我的问题是我的形象的名称。我不能在res文件夹中使用大写字母。我必须将名称从noImage.png更改为noimage.png并且每个人都是正确的,因为我在android中调用它时不使用.png。