简体   繁体   English

NullpointerException 在 Canvas 上绘制 Bitmap

[英]NullpointerException drawing Bitmap on Canvas

Why do i get a NullpointerException?为什么我会收到 NullpointerException?

Thank you!谢谢!

ps: I am new to Java. 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;
    }

the output: output:

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

This may be silly but did you initialize canvas somewhere previously?这可能很愚蠢,但是您之前是否在某个地方初始化了 canvas?

I'm thinking you did not initialize the canvas object.我在想你没有初始化 canvas object。

is that one in the overriden onDraw (Canvas canvas) method?是覆盖 onDraw (Canvas canvas) 方法中的那个吗?

InputStream is an abstract class, i dont think you can create an instance of it, have you tried: InputStream 是一个抽象的 class,我认为您无法创建它的实例,您是否尝试过:

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

I'm not that good in game development, but why aren't you extending from SurfaceView and implementing Surface.CallBack and use inner AsyncTask class for handling threads?我在游戏开发方面不是那么好,但你为什么不从 SurfaceView 扩展并实现 Surface.CallBack 并使用内部 AsyncTask class 来处理线程? Does the job nicely!工作做得很好! =] =]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM