我是android的初学者,我试图在在线视频教程的帮助下制作基本游戏。我有多精灵问题以及如何使用surfaceview。应用程序启动失败。 这是游戏的代码。请帮助我。
package com.example.killthemall;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Toast;
public class Game extends Activity {
KhogenView View1;
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
while(true){
try {
OurThread.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}}
}
Thread OurThread;
int herorows = 4;
int herocolumns = 3;
int xpos, ypos;
int xspeed;
int yspeed;
int herowidth;
int widthnumber = 0;
int heroheight;
Rect src;
Rect dst;
int round;
Bitmap bmp1;
// private Bitmap bmp1;//change name
public List<Sprite> sprites = new ArrayList<Sprite>() {
};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
View1 = new KhogenView(this);
setContentView(View1);
sprites.add(createSprite(R.drawable.image));
sprites.add(createSprite(R.drawable.bad1));
sprites.add(createSprite(R.drawable.bad2));
sprites.add(createSprite(R.drawable.bad3));
sprites.add(createSprite(R.drawable.bad4));
sprites.add(createSprite(R.drawable.bad5));
sprites.add(createSprite(R.drawable.bad6));
sprites.add(createSprite(R.drawable.good1));
sprites.add(createSprite(R.drawable.good2));
sprites.add(createSprite(R.drawable.good3));
sprites.add(createSprite(R.drawable.good4));
sprites.add(createSprite(R.drawable.good5));
sprites.add(createSprite(R.drawable.good6));
}
private Sprite createSprite(int image) {
// TODO Auto-generated method stub
bmp1 = BitmapFactory.decodeResource(getResources(), image);
return new Sprite(this, bmp1);
}
public class KhogenView extends SurfaceView implements Runnable {
SurfaceHolder OurHolder;
Canvas canvas = null;
Random rnd = new Random();
{
xpos = rnd.nextInt(canvas.getWidth() - herowidth)+herowidth;
ypos = rnd.nextInt(canvas.getHeight() - heroheight)+heroheight;
xspeed = rnd.nextInt(10 - 5) + 5;
yspeed = rnd.nextInt(10 - 5) + 5;
}
public KhogenView(Context context) {
super(context);
// TODO Auto-generated constructor stub
OurHolder = getHolder();
OurThread = new Thread(this);
OurThread.start();
}
@Override
public void run() {
// TODO Auto-generated method stub
herowidth = bmp1.getWidth() / 3;
heroheight = bmp1.getHeight() / 4;
boolean isRunning = true;
while (isRunning) {
if (!OurHolder.getSurface().isValid())
continue;
canvas = OurHolder.lockCanvas();
canvas.drawRGB(02, 02, 50);
for (Sprite sprite : sprites) {
if (widthnumber == 3)
widthnumber = 0;
update();
getdirection();
src = new Rect(widthnumber * herowidth, round * heroheight,
(widthnumber + 1) * herowidth, (round + 1)* heroheight);
dst = new Rect(xpos, ypos, xpos + herowidth, ypos+ heroheight);
canvas.drawBitmap(bmp1, src, dst, null);
}
widthnumber++;
OurHolder.unlockCanvasAndPost(canvas);
}
}
public void update() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (xpos + xspeed <= 0)
xspeed = 40;
if (xpos >= canvas.getWidth() - herowidth)
xspeed = -50;
if (ypos + yspeed <= 0)
yspeed = 45;
if (ypos >= canvas.getHeight() - heroheight)
yspeed = -55;
xpos = xpos + xspeed;
ypos = ypos + yspeed;
}
public void getdirection() {
double angleinteger = (Math.atan2(yspeed, xspeed)) / (Math.PI / 2);
round = (int) (Math.round(angleinteger) + 2) % herorows;
// Toast.makeText(this, String.valueOf(round),
// Toast.LENGTH_LONG).show();
}
}
public class Sprite {
Game game;
private Bitmap bmp;
public Sprite(Game game, Bitmap bmp) {
// TODO Auto-generated constructor stub
this.game = game;
this.bmp = bmp;
}
}
}
如果它有帮助,这是logCat ......
08-22 23:18:06.980: D/AndroidRuntime(28151): Shutting down VM
08-22 23:18:06.980: W/dalvikvm(28151): threadid=1: thread exiting with uncaught exception (group=0xb3f6f4f0)
08-22 23:18:06.980: D/AndroidRuntime(28151): procName from cmdline: com.example.killthemall
08-22 23:18:06.980: E/AndroidRuntime(28151): in writeCrashedAppName, pkgName :com.example.killthemall
08-22 23:18:06.980: D/AndroidRuntime(28151): file written successfully with content: com.example.killthemall StringBuffer : ;com.example.killthemall
08-22 23:18:06.990: I/Process(28151): Sending signal. PID: 28151 SIG: 9
08-22 23:18:06.990: E/AndroidRuntime(28151): FATAL EXCEPTION: main
08-22 23:18:06.990: E/AndroidRuntime(28151): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.killthemall/com.example.killthemall.Game}: java.lang.NullPointerException
08-22 23:18:06.990: E/AndroidRuntime(28151): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
08-22 23:18:06.990: E/AndroidRuntime(28151): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
08-22 23:18:06.990: E/AndroidRuntime(28151): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-22 23:18:06.990: E/AndroidRuntime(28151): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
08-22 23:18:06.990: E/AndroidRuntime(28151): at android.os.Handler.dispatchMessage(Handler.java:99)
08-22 23:18:06.990: E/AndroidRuntime(28151): at android.os.Looper.loop(Looper.java:130)
08-22 23:18:06.990: E/AndroidRuntime(28151): at android.app.ActivityThread.main(ActivityThread.java:3683)
08-22 23:18:06.990: E/AndroidRuntime(28151): at java.lang.reflect.Method.invokeNative(Native Method)
08-22 23:18:06.990: E/AndroidRuntime(28151): at java.lang.reflect.Method.invoke(Method.java:507)
08-22 23:18:06.990: E/AndroidRuntime(28151): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:880)
08-22 23:18:06.990: E/AndroidRuntime(28151): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:638)
08-22 23:18:06.990: E/AndroidRuntime(28151): at dalvik.system.NativeStart.main(Native Method)
08-22 23:18:06.990: E/AndroidRuntime(28151): Caused by: java.lang.NullPointerException
08-22 23:18:06.990: E/AndroidRuntime(28151): at com.example.killthemall.Game$KhogenView.<init>(Game.java:96)
08-22 23:18:06.990: E/AndroidRuntime(28151): at com.example.killthemall.Game.onCreate(Game.java:58)
08-22 23:18:06.990: E/AndroidRuntime(28151): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
08-22 23:18:06.990: E/AndroidRuntime(28151): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
08-22 23:18:06.990: E/AndroidRuntime(28151): ... 11 more
08-22 23:18:18.050: D/AndroidRuntime(28191): Shutting down VM
08-22 23:18:18.050: W/dalvikvm(28191): threadid=1: thread exiting with uncaught exception (group=0xb3f6f4f0)
08-22 23:18:18.050: I/Process(28191): Sending signal. PID: 28191 SIG: 9
08-22 23:18:18.050: D/AndroidRuntime(28191): procName from cmdline: com.example.killthemall
08-22 23:18:18.050: E/AndroidRuntime(28191): in writeCrashedAppName, pkgName :com.example.killthemall
08-22 23:18:18.050: D/AndroidRuntime(28191): file written successfully with content: com.example.killthemall StringBuffer : ;com.example.killthemall
08-22 23:18:18.050: E/AndroidRuntime(28191): FATAL EXCEPTION: main
08-22 23:18:18.050: E/AndroidRuntime(28191): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.killthemall/com.example.killthemall.Game}: java.lang.NullPointerException
08-22 23:18:18.050: E/AndroidRuntime(28191): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
08-22 23:18:18.050: E/AndroidRuntime(28191): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
08-22 23:18:18.050: E/AndroidRuntime(28191): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-22 23:18:18.050: E/AndroidRuntime(28191): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
08-22 23:18:18.050: E/AndroidRuntime(28191): at android.os.Handler.dispatchMessage(Handler.java:99)
08-22 23:18:18.050: E/AndroidRuntime(28191): at android.os.Looper.loop(Looper.java:130)
08-22 23:18:18.050: E/AndroidRuntime(28191): at android.app.ActivityThread.main(ActivityThread.java:3683)
08-22 23:18:18.050: E/AndroidRuntime(28191): at java.lang.reflect.Method.invokeNative(Native Method)
08-22 23:18:18.050: E/AndroidRuntime(28191): at java.lang.reflect.Method.invoke(Method.java:507)
08-22 23:18:18.050: E/AndroidRuntime(28191): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:880)
08-22 23:18:18.050: E/AndroidRuntime(28191): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:638)
08-22 23:18:18.050: E/AndroidRuntime(28191): at dalvik.system.NativeStart.main(Native Method)
08-22 23:18:18.050: E/AndroidRuntime(28191): Caused by: java.lang.NullPointerException
08-22 23:18:18.050: E/AndroidRuntime(28191): at com.example.killthemall.Game$KhogenView.<init>(Game.java:96)
08-22 23:18:18.050: E/AndroidRuntime(28191): at com.example.killthemall.Game.onCreate(Game.java:58)
08-22 23:18:18.050: E/AndroidRuntime(28191): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
08-22 23:18:18.050: E/AndroidRuntime(28191): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
08-22 23:18:18.050: E/AndroidRuntime(28191): ... 11 more
答案 0 :(得分:2)
我决定引导您通过调试过程,而不是只是让您知道问题出在哪里,以便下次遇到错误的异常时,您将更适合处理它们。所以问题的秘密在于你的LogCat,相关的线路告诉了我们很多:
Caused by: java.lang.NullPointerException
at com.example.killthemall.Game$KhogenView.<init>(Game.java:96)
at com.example.killthemall.Game.onCreate(Game.java:58)
这是第96行:
xpos = rnd.nextInt(canvas.getWidth() - herowidth)+herowidth;
这里没有什么太明显/不同寻常的了。现在让我们检查这些变量是否/如何初始化
首先是xpos
int xpos;
看起来很好,现在让我们看一下canvas
。
Canvas canvas = null;
正确的是你的问题。 canvas
在使用之前被初始化为 null 两行,因此当您尝试调用getWidth()
时会抛出异常;更具体地说,是一个NullPointerException,因为canvas指向 null 而不是Canvas对象。
我不确定您的应用计划到底是什么,但根据相关的Canvas and Drawable文档,他们建议初始化Canvas
:
Bitmap b = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(b);
同样,我不确定Canvas
的用例,但初始化它应该排除第96行的问题。希望这有帮助。