我是Android Studio和StackOverflow的新手,请提前原谅我。
我试着制作一个简单的android程序,输出自己的帧速率。
当我运行它时,会出现以下错误:
Attempt to invoke interface method 'android.graphics.Canvas android.view.SurfaceHolder.lockCanvas()' on a null object reference
我相当肯定这是该计划的问题领域:
while(running){
startTime=System.nanoTime();
canvas=null;
try{
canvas=this.surfaceHolder.lockCanvas();
synchronized(surfaceHolder){
this.gamePanel.update();
this.gamePanel.draw(canvas);
}
}catch(Exception e) {
e.printStackTrace();
}finally{
if(canvas!=null){
try{
surfaceHolder.unlockCanvasAndPost(canvas);
}catch(Exception e) {
e.printStackTrace();}
}
}
我直接从本教程中复制了它:
https://www.youtube.com/watch?v=OojQitoAEXs
据我所知,我逐行复制了它。你知道我如何修复这个错误吗?
以下是整个程序,如果有帮助的话(很抱歉转储):
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.Activity;
import android.view.Window;
import android.view.WindowManager;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(new GamePanel(this));
}
}
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.MotionEvent;
import android.graphics.Canvas;
import android.content.Context;
import java.lang.Thread;
public class GamePanel extends SurfaceView implements SurfaceHolder.Callback
{
private MainThread thread;
public GamePanel(Context context){
super(context);
getHolder().addCallback(this);
thread=new MainThread(getHolder(), this);
setFocusable(true);
}
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height){
}
public void surfaceCreated(SurfaceHolder holder){
thread=new MainThread(getHolder(), this);
thread.setRunning(true);
thread.start();
}
public void surfaceDestroyed(SurfaceHolder holder){
boolean retry=true;
while(true){
try{
thread.setRunning(false);
thread.join();
}catch(Exception e){
e.printStackTrace();}
retry=false;
}
}
public boolean onTouchEvent(MotionEvent event) {
return super.onTouchEvent(event);
}
public void update(){
}
public void draw(Canvas canvas) {
super.draw(canvas);
}
}
import android.view.SurfaceHolder;
import android.graphics.Canvas;
public class MainThread extends Thread {
public static final int MAX_FPS = 30;
private double averageFPS;
private SurfaceHolder surfaceHolder;
private GamePanel gamePanel;
private boolean running;
public static Canvas canvas;
public void setRunning(boolean running){
this.running=running;
}
public MainThread(SurfaceHolder surfaceholder, GamePanel gamepanel){
super();
this.surfaceHolder=surfaceHolder;
this.gamePanel=gamePanel;
}
public void run(){
long startTime;
long timeMillis=1000/MAX_FPS;
long waitTime;
int frameCount=0;
long totalTime=0;
long targetTime=1000/MAX_FPS;
while(running){
startTime=System.nanoTime();
canvas=null;
try{
canvas=this.surfaceHolder.lockCanvas();
synchronized(surfaceHolder){
this.gamePanel.update();
this.gamePanel.draw(canvas);
}
}catch(Exception e) {
e.printStackTrace();
}finally{
if(canvas!=null){
try{
surfaceHolder.unlockCanvasAndPost(canvas);
}catch(Exception e) {
e.printStackTrace();}
}
}
timeMillis=(System.nanoTime()-startTime)/1000000;
waitTime=targetTime-timeMillis;
try{
if(waitTime>0){
this.sleep(waitTime);
}
}catch(Exception e) {
e.printStackTrace();
}
totalTime+=System.nanoTime()-startTime;
frameCount++;
if(frameCount==MAX_FPS){
averageFPS=1000/(totalTime/frameCount)/1000000;
frameCount=0;
totalTime=0;
System.out.println(averageFPS);
}
}
}
}