gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 10:05:09 26 4
gpt4 key购买 nike

运行此脚本时:

    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 行(位于最后一个名为 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;

或者日志是否指向其他内容?我目前正在学习如何设置 Sprite 动画的教程,所以我还不太了解日志。

最佳答案

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

关于java - 尝试在空对象引用上调用虚拟方法(JAVA、AS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36533590/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com