0

为什么我会收到 NullpointerException?

谢谢!

ps:我是Java新手。

    try {
        URL uri = new URL("http://static.panoramio.com/photos/original/41455707.jpg");
            URLConnection connection = uri.openConnection();
            Log.i(TAG, "connecting...");
            connection.connect();
            InputStream is = connection.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is, 8 * 1024);
            Bitmap bmp = BitmapFactory.decodeStream(bis);
            bis.close();
            is.close();

            Log.i(TAG, "setting bitmap");
            //Log.d(TAG, canvas.toString());
            //Log.d(TAG, bmp.toString());
            canvas.setBitmap(bmp);
    } catch (Exception exc){
        Log.e(TAG, exc.toString());
        return;
    }

输出:

06-28 17:29:04.857   391   397 I MyWallpaperPainting: connecting...
06-28 17:29:07.248   391   397 I MyWallpaperPainting: setting bitmap
06-28 17:29:07.248   391   397 E MyWallpaperPainting: java.lang.NullPointerException
4

5 回答 5

1

这可能很愚蠢,但是您之前是否在某个地方初始化了画布?

于 2011-06-28T15:36:04.943 回答
0

我在想你没有初始化画布对象。

于 2011-06-28T15:35:34.277 回答
0

我在游戏开发方面不是那么好,但是你为什么不从 SurfaceView 扩展并实现 Surface.CallBack 并使用内部 AsyncTask 类来处理线程?工作做得很好!=]

于 2011-06-28T22:53:57.113 回答
0

是覆盖 onDraw (Canvas canvas) 方法中的那个吗?

于 2011-06-28T15:39:38.597 回答
0

InputStream 是一个抽象类,我认为您不能创建它的实例,您是否尝试过:

BufferedInputStream bis = new BufferedInputStream(new InputStream(connection.getInputStream()), 8 * 1024);

于 2011-06-28T16:17:20.083 回答