开发者

Null Pointer Exception when Drawing an image

I am having a problem with setting an ImageView image to an image which has been fetched from a URL. Here is the method used to turn the URL into a Drawable object:

private Drawable LoadImage(String url){
            try
            {
                InputStream is = (InputStream) new URL(url).getContent();
                Drawable d = Drawable.createFromStream(is, "src");
                return d;
            }catch (Exception e) {
                System.out.println("Exc="+e);
                return null;
            }
    }

Here is the code to call the method:

Drawable drawable = LoadImage("www.myurl.com/image.jpg");
imageView.setImageDrawable(drawable); //here is where I think it goes wrong

It gives me a Null Pointer Exception but when I display the drawable.toString() variable in a text view I get something like this开发者_运维知识库:

android.graphics.drawable.BitMapDrawable@43e5bb18

So it shows it's not returning null and fails when it tries to draw it. Am I missing something in the manifest or is there something wrong with my code?


First of all... don't use System.out on Android. Use Log instead.

I assume your LoadImage function returns null.

Take a look at the documentation about what ddms and logcat is and than try to get some more information about what happens.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜