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

时间:2016-04-10 18:13:51

标签: java android android-studio logcat

运行此脚本时:

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

我收到错误消息:

  

致命异常:线程-106                                                                             处理:com.example.benjamin.labb3,PID:2169                                                                             java.lang.NullPointerException:尝试调用虚方法&#39; boolean android.graphics.Bitmap.isRecycled()&#39;在null对象引用上                                                                                 在android.graphics.Canvas.throwIfCannotDraw(Canvas.java:1269)                                                                                 在android.graphics.Canvas.drawBitmap(Canvas.java:1325)                                                                                 在com.example.benjamin.labb3.Main $ DrawView.run(Main.java:97)                                                                                 在java.lang.Thread.run(Thread.java:818)

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

第97行:frame = 0;

错误日志表明它的&#34; null&#34;所以我检查框架是否被声明,结果它已经是,所以我怎么能得到一个错误,说它是空的?

我在这里宣布:

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;

或者日志是否还有其他东西?我目前正在学习如何设置精灵动画的教程,所以我还不太了解日志。

1 个答案:

答案 0 :(得分:1)

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

相关问题