绘制图像时空指针异常 [英] Null Pointer Exception when Drawing an image

查看:128
本文介绍了绘制图像时空指针异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ImageView的图像设置到已经从URL提取的图像的问题。下面是用来打开网址进入一个可绘制对象方法:

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

下面是code调用的方法:

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

它给了我一个空指针异常,但是当我在一个文本视图显示drawable.toString()可变我得到的是这样的:

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

所以它显示它没有返回null,当它试图绘制失败。我缺少的东西表现还是有我的code什么不对?

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?

推荐答案

首先...不要在Android上使用的System.out。使用登录来代替。

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

我假设你的LoadImage函数返回null。

I assume your LoadImage function returns null.

在什么DDMS和logcat的是文档看一看,不是试图获得有关会发生什么情况的更多信息。

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

这篇关于绘制图像时空指针异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆