【问题标题】:Attempt to invoke virtual method on null object reference (JAVA, AS)尝试在空对象引用(JAVA、AS)上调用虚方法
【发布时间】:2016-04-11 02:27:41
【问题描述】:

运行此脚本时:

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

我收到错误消息:

致命异常:Thread-106 进程:com.example.benjamin.labb3,PID:2169 java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“boolean android.graphics.Bitmap.isRecycled()” 在 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;

错误日志说它是“null”,所以我检查是否声明了框架,结果证明它已经声明了,那么我怎样才能得到一个错误,说它是 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;

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

【问题讨论】:

    标签: java android android-studio logcat


    【解决方案1】:

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

    【讨论】:

    • 原来 png 文件“incect”是用大写的 i 拼写的,所以我把它改成了“Incect”,它工作了!
    猜你喜欢
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    • 2021-11-07
    • 1970-01-01
    • 1970-01-01
    • 2019-05-10
    • 2015-06-26
    相关资源
    最近更新 更多