尝试在空对象引用上调用虚拟方法 (JAVA, AS)

Attempt to invoke virtual method on null object reference (JAVA, AS)

当运行安装此脚本时:

    package com.example.benjamin.labb3;

import android.app.Activity;
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;
import java.io.IOException;
import java.io.InputStream;

public class Main extends Activity {

    DrawView drawView;

    @Override public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        drawView = new DrawView(this);
        setContentView(drawView);
    }

    @Override public void onResume(){
        super.onResume();
        drawView.resume();
    }

    @Override public void onPause(){
        super.onPause();
        drawView.pause();
    }

    public class  DrawView extends SurfaceView implements Runnable {
        Thread gameloop = null;
        SurfaceHolder surface;
        volatile boolean running = false;
        AssetManager assets = null;
        BitmapFactory.Options options = null;
        Bitmap incect[];
        int frame = 0;

        public DrawView(Context context){
            super(context);
            surface = getHolder();
            assets = context.getAssets();
            options = new BitmapFactory.Options();
            options.inPreferredConfig = Bitmap.Config.ARGB_8888;

            incect = new Bitmap[2];

            try {
                for (int n = 0; n < 2; n++){
                    String fileName = "Incect"+Integer.toString(n+1)+".png";
                    InputStream istream = assets.open(fileName);
                    incect[n] = BitmapFactory.decodeStream(istream,null,options);
                    istream.close();
                }
            } catch (IOException e){
                e.printStackTrace();
            }
        }

        public void resume() {
            running = true;
            gameloop = new Thread(this);
            gameloop.start();
        }

        public void pause() {
            running = false;
            boolean retry = true;
            while (retry) {
                try {
                    gameloop.join();
                    retry = false;
                } catch (InterruptedException e){}
            }
        }

        @Override public void run(){
            while (running){

                if(!surface.getSurface().isValid())
                    continue;
                Canvas canvas = surface.lockCanvas();
                canvas.drawColor(Color.rgb(85,107,47));
                canvas.drawBitmap(incect[frame],0,0,null);
                surface.unlockCanvasAndPost(canvas);
                frame ++;
                if (frame > 1){
                    frame = 0;
                }
            }
        }
    }
}

我收到错误消息:

FATAL EXCEPTION: Thread-106 Process: com.example.benjamin.labb3, PID: 2169 java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.Bitmap.isRecycled()' on a null object reference at android.graphics.Canvas.throwIfCannotDraw(Canvas.java:1269) at android.graphics.Canvas.drawBitmap(Canvas.java:1325) at com.example.benjamin.labb3.Main$DrawView.run(Main.java:97) at java.lang.Thread.run(Thread.java:818)

所以我导航到错误所在的第 97 行(在最后一个名为 运行() 的方法中):

第 97 行:frame = 0;

错误日志说它 "null" 所以我检查是否声明了框架,结果发现它已经是,那么我怎么会得到一个错误说它是空的?

我在这里声明:

public class  DrawView extends SurfaceView implements Runnable {
        Thread gameloop = null;
        SurfaceHolder surface;
        volatile boolean running = false;
        AssetManager assets = null;
        BitmapFactory.Options options = null;
        Bitmap incect[];
        int frame = 0;

或者日志引用了其他内容?我目前正在学习如何设置精灵动画的教程,所以我还不太了解日志。

incect[frame] 可能是 null。检查 bitmap 是否存在于数组中的特定位置。