我正在eclipse中开发一款针对android的whack-a-mole游戏。我的应用程序适用于Android 2.2但不适用于较新版本。 发生的事情是,启动画面加载正常,我的主要活动也开始正常,但要开始游戏,您需要点击主要活动的第一个屏幕。当你这样做时,模拟器显示:“不幸我的应用程序已停止”
我遇到了类似的问题here,但如果这是我的代码中的问题,我无法找到我必须做的事情。
logcat的:
07-04 12:08:49.029: E/InputEventReceiver(2099): Exception dispatching input event.
07-04 12:08:49.049: E/MessageQueue-JNI(2099): Exception in MessageQueue callback: handleReceiveCallback
07-04 12:08:49.329: E/MessageQueue-JNI(2099): java.lang.NullPointerException
07-04 12:08:49.329: E/MessageQueue-JNI(2099): at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:589)
07-04 12:08:49.329: E/MessageQueue-JNI(2099): at com.jellyproductions.holymoly.HolyMolyView$WhackAMoleThread.doTouchEvent(HolyMolyView.java:460)
07-04 12:08:49.329: E/MessageQueue-JNI(2099): at com.jellyproductions.holymoly.HolyMolyView.onTouchEvent(HolyMolyView.java:1691)
07-04 12:08:49.329: E/MessageQueue-JNI(2099): at android.view.View.dispatchTouchEvent(View.java:7706)
07-04 12:08:49.329: E/MessageQueue-JNI(2099): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
07-04 12:08:49.329: E/MessageQueue-JNI(2099): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
07-04 12:08:49.329: E/MessageQueue-JNI(2099): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
07-04 12:08:49.329: E/MessageQueue-JNI(2099): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
07-04 12:08:49.329: E/MessageQueue-JNI(2099): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
07-04 12:08:49.329: E/MessageQueue-JNI(2099): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
07-04 12:08:49.329: E/MessageQueue-JNI(2099): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
07-04 12:08:49.329: E/MessageQueue-JNI(2099): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
07-04 12:08:49.329: E/MessageQueue-JNI(2099): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2068)
07-04 12:08:49.329: E/MessageQueue-JNI(2099): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1515)
07-04 12:08:49.329: E/MessageQueue-JNI(2099): at android.app.Activity.dispatchTouchEvent(Activity.java:2458)
07-04 12:08:49.329: E/MessageQueue-JNI(2099): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2016)
07-04 12:08:49.329: E/MessageQueue-JNI(2099): at android.view.View.dispatchPointerEvent(View.java:7886)
07-04 12:08:49.329: E/MessageQueue-JNI(2099): at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:3954)
07-04 12:08:49.329: E/MessageQueue-JNI(2099): at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3833)
07-04 12:08:49.329: E/MessageQueue-JNI(2099): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
07-04 12:08:49.329: E/MessageQueue-JNI(2099): at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3449)
07-04 12:08:49.329: E/MessageQueue-JNI(2099): at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3418)
07-04 12:08:49.329: E/MessageQueue-JNI(2099): at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3525)
07-04 12:08:49.329: E/MessageQueue-JNI(2099): at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3426)
07-04 12:08:49.329: E/MessageQueue-JNI(2099): at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3582)
07-04 12:08:49.329: E/MessageQueue-JNI(2099): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
07-04 12:08:49.329: E/MessageQueue-JNI(2099): at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3449)
07-04 12:08:49.329: E/MessageQueue-JNI(2099): at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3418)
07-04 12:08:49.329: E/MessageQueue-JNI(2099): at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3426)
07-04 12:08:49.329: E/MessageQueue-JNI(2099): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
07-04 12:08:49.329: E/MessageQueue-JNI(2099): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5602)
07-04 12:08:49.329: E/MessageQueue-JNI(2099): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5582)
07-04 12:08:49.329: E/MessageQueue-JNI(2099): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5553)
07-04 12:08:49.329: E/MessageQueue-JNI(2099): at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5682)
07-04 12:08:49.329: E/MessageQueue-JNI(2099): at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
07-04 12:08:49.329: E/MessageQueue-JNI(2099): at android.os.MessageQueue.nativePollOnce(Native Method)
07-04 12:08:49.329: E/MessageQueue-JNI(2099): at android.os.MessageQueue.next(MessageQueue.java:138)
07-04 12:08:49.329: E/MessageQueue-JNI(2099): at android.os.Looper.loop(Looper.java:123)
07-04 12:08:49.329: E/MessageQueue-JNI(2099): at android.app.ActivityThread.main(ActivityThread.java:5017)
07-04 12:08:49.329: E/MessageQueue-JNI(2099): at java.lang.reflect.Method.invokeNative(Native Method)
07-04 12:08:49.329: E/MessageQueue-JNI(2099): at java.lang.reflect.Method.invoke(Method.java:515)
07-04 12:08:49.329: E/MessageQueue-JNI(2099): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-04 12:08:49.329: E/MessageQueue-JNI(2099): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-04 12:08:49.329: E/MessageQueue-JNI(2099): at dalvik.system.NativeStart.main(Native Method)
07-04 12:08:49.339: D/AndroidRuntime(2099): Shutting down VM
07-04 12:08:49.339: W/dalvikvm(2099): threadid=1: thread exiting with uncaught exception (group=0xb3a4cba8)
07-04 12:08:49.569: E/AndroidRuntime(2099): FATAL EXCEPTION: main
07-04 12:08:49.569: E/AndroidRuntime(2099): Process: com.jellyproductions.holymoly, PID: 2099
07-04 12:08:49.569: E/AndroidRuntime(2099): java.lang.NullPointerException
07-04 12:08:49.569: E/AndroidRuntime(2099): at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:589)
07-04 12:08:49.569: E/AndroidRuntime(2099): at com.jellyproductions.holymoly.HolyMolyView$WhackAMoleThread.doTouchEvent(HolyMolyView.java:460)
07-04 12:08:49.569: E/AndroidRuntime(2099): at com.jellyproductions.holymoly.HolyMolyView.onTouchEvent(HolyMolyView.java:1691)
07-04 12:08:49.569: E/AndroidRuntime(2099): at android.view.View.dispatchTouchEvent(View.java:7706)
07-04 12:08:49.569: E/AndroidRuntime(2099): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
07-04 12:08:49.569: E/AndroidRuntime(2099): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
07-04 12:08:49.569: E/AndroidRuntime(2099): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
07-04 12:08:49.569: E/AndroidRuntime(2099): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
07-04 12:08:49.569: E/AndroidRuntime(2099): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
07-04 12:08:49.569: E/AndroidRuntime(2099): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
07-04 12:08:49.569: E/AndroidRuntime(2099): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
07-04 12:08:49.569: E/AndroidRuntime(2099): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
07-04 12:08:49.569: E/AndroidRuntime(2099): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2068)
07-04 12:08:49.569: E/AndroidRuntime(2099): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1515)
07-04 12:08:49.569: E/AndroidRuntime(2099): at android.app.Activity.dispatchTouchEvent(Activity.java:2458)
07-04 12:08:49.569: E/AndroidRuntime(2099): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2016)
07-04 12:08:49.569: E/AndroidRuntime(2099): at android.view.View.dispatchPointerEvent(View.java:7886)
07-04 12:08:49.569: E/AndroidRuntime(2099): at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:3954)
07-04 12:08:49.569: E/AndroidRuntime(2099): at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3833)
07-04 12:08:49.569: E/AndroidRuntime(2099): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
07-04 12:08:49.569: E/AndroidRuntime(2099): at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3449)
07-04 12:08:49.569: E/AndroidRuntime(2099): at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3418)
07-04 12:08:49.569: E/AndroidRuntime(2099): at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3525)
07-04 12:08:49.569: E/AndroidRuntime(2099): at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3426)
07-04 12:08:49.569: E/AndroidRuntime(2099): at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3582)
07-04 12:08:49.569: E/AndroidRuntime(2099): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
07-04 12:08:49.569: E/AndroidRuntime(2099): at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3449)
07-04 12:08:49.569: E/AndroidRuntime(2099): at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3418)
07-04 12:08:49.569: E/AndroidRuntime(2099): at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3426)
07-04 12:08:49.569: E/AndroidRuntime(2099): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3399)
07-04 12:08:49.569: E/AndroidRuntime(2099): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5602)
07-04 12:08:49.569: E/AndroidRuntime(2099): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5582)
07-04 12:08:49.569: E/AndroidRuntime(2099): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5553)
07-04 12:08:49.569: E/AndroidRuntime(2099): at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5682)
07-04 12:08:49.569: E/AndroidRuntime(2099): at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
07-04 12:08:49.569: E/AndroidRuntime(2099): at android.os.MessageQueue.nativePollOnce(Native Method)
07-04 12:08:49.569: E/AndroidRuntime(2099): at android.os.MessageQueue.next(MessageQueue.java:138)
07-04 12:08:49.569: E/AndroidRuntime(2099): at android.os.Looper.loop(Looper.java:123)
07-04 12:08:49.569: E/AndroidRuntime(2099): at android.app.ActivityThread.main(ActivityThread.java:5017)
07-04 12:08:49.569: E/AndroidRuntime(2099): at java.lang.reflect.Method.invokeNative(Native Method)
07-04 12:08:49.569: E/AndroidRuntime(2099): at java.lang.reflect.Method.invoke(Method.java:515)
07-04 12:08:49.569: E/AndroidRuntime(2099): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-04 12:08:49.569: E/AndroidRuntime(2099): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-04 12:08:49.569: E/AndroidRuntime(2099): at dalvik.system.NativeStart.main(Native Method)
DoTouchEvent方法:
boolean doTouchEvent(MotionEvent event) {
synchronized (mySurfaceHolder) {
int eventaction = event.getAction();
int X = (int) event.getX();
int Y = (int) event.getY();
switch (eventaction) {
case MotionEvent.ACTION_DOWN:
if (!gameOver) {
fingerX = X;
fingerY = Y;
if (!onTitle && detectMoleHit()) {
whacking = true;
molesWhacked++;
if (molesWhacked < highscore) {
highscore = highscore;
} else if (molesWhacked == highscore) {
molesWhacked = highscore;
} else if (molesWhacked > highscore) {
highscore++;
}
}
}
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
if (onTitle || gameOver || gameOverRed || gameOverBlue
|| gameOverGreen || gameOverPink || gameOverYellow) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
backgroundImg = BitmapFactory.decodeResource(
myContext.getResources(),
R.drawable.background, options);
backgroundImg = Bitmap.createScaledBitmap( **//Line 460**
backgroundImg, screenW, screenH, true);
maskup = BitmapFactory.decodeResource(
myContext.getResources(), R.drawable.maskup,
options);
maskmiddle = BitmapFactory.decodeResource(
myContext.getResources(),
R.drawable.maskmiddle, options);
maskdown = BitmapFactory.decodeResource(
myContext.getResources(), R.drawable.maskdown,
options);
molered = BitmapFactory.decodeResource(
myContext.getResources(), R.drawable.molered,
options);
moleblue = BitmapFactory.decodeResource(
myContext.getResources(), R.drawable.moleblue,
options);
molegreen = BitmapFactory.decodeResource(
myContext.getResources(), R.drawable.molegreen,
options);
molepink = BitmapFactory.decodeResource(
myContext.getResources(), R.drawable.molepink,
options);
moleyellow = BitmapFactory.decodeResource(
myContext.getResources(),
R.drawable.moleyellow, options);
whack = BitmapFactory.decodeResource(
myContext.getResources(), R.drawable.whack,
options);
gameoverred = BitmapFactory.decodeResource(
myContext.getResources(),
R.drawable.gameoverred, options);
gameoverblue = BitmapFactory.decodeResource(
myContext.getResources(),
R.drawable.gameoverblue, options);
gameovergreen = BitmapFactory.decodeResource(
myContext.getResources(),
R.drawable.gameovergreen, options);
gameoverpink = BitmapFactory.decodeResource(
myContext.getResources(),
R.drawable.gameoverpink, options);
gameoveryellow = BitmapFactory.decodeResource(
myContext.getResources(),
R.drawable.gameoveryellow, options);
scaleW = (float) screenW / (float) backgroundOrigW;
scaleH = (float) screenH / (float) backgroundOrigH;
maskup = Bitmap.createScaledBitmap(maskup,
(int) (maskup.getWidth() * scaleW),
(int) (maskup.getHeight() * scaleH), true);
maskmiddle = Bitmap.createScaledBitmap(maskmiddle,
(int) (maskmiddle.getWidth() * scaleW),
(int) (maskmiddle.getHeight() * scaleH), true);
maskdown = Bitmap.createScaledBitmap(maskdown,
(int) (maskdown.getWidth() * scaleW),
(int) (maskdown.getHeight() * scaleH), true);
molered = Bitmap.createScaledBitmap(molered,
(int) (molered.getWidth() * scaleW),
(int) (molered.getHeight() * scaleH), true);
moleblue = Bitmap.createScaledBitmap(moleblue,
(int) (moleblue.getWidth() * scaleW),
(int) (moleblue.getHeight() * scaleH), true);
molegreen = Bitmap.createScaledBitmap(molegreen,
(int) (molegreen.getWidth() * scaleW),
(int) (molegreen.getHeight() * scaleH), true);
molepink = Bitmap.createScaledBitmap(molepink,
(int) (molepink.getWidth() * scaleW),
(int) (molepink.getHeight() * scaleH), true);
moleyellow = Bitmap.createScaledBitmap(moleyellow,
(int) (moleyellow.getWidth() * scaleW),
(int) (moleyellow.getHeight() * scaleH), true);
whack = Bitmap.createScaledBitmap(whack,
(int) (whack.getWidth() * scaleW),
(int) (whack.getHeight() * scaleH), true);
gameoverred = Bitmap.createScaledBitmap(gameoverred,
screenW, screenH, true);
gameoverblue = Bitmap.createScaledBitmap(gameoverblue,
screenW, screenH, true);
gameovergreen = Bitmap.createScaledBitmap(
gameovergreen, screenW, screenH, true);
gameoverpink = Bitmap.createScaledBitmap(gameoverpink,
screenW, screenH, true);
gameoveryellow = Bitmap.createScaledBitmap(
gameoveryellow, screenW, screenH, true);
onTitle = false;
pickMole();
}
whacking = false;
if (gameOver || gameOverRed || gameOverBlue
|| gameOverGreen || gameOverPink || gameOverYellow) {
if (molesWhacked > highscore) {
molesWhacked = highscore;
} else if (molesWhacked == highscore) {
molesWhacked = highscore;
} else if (molesWhacked < highscore) {
highscore = highscore;
}
molesWhacked = 0;
molesMissed = 0;
activeMoleR = 0;
activeMoleB = 0;
activeMoleG = 0;
activeMoleP = 0;
activeMoleY = 0;
pickMoleColor();
gameOver = false;
gameOverRed = false;
gameOverBlue = false;
gameOverGreen = false;
gameOverPink = false;
gameOverYellow = false;
pickMole();
}
break;
} **//Line 589**
}
return true;
}
OnTouchEvent方法:
@Override
public boolean onTouchEvent(MotionEvent event) {
return thread.doTouchEvent(event); **//Line 1691**
}
主要活动XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<com.jellyproductions.holymoly.HolyMolyView
android:id="@+id/mole"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<com.google.ads.AdView android:id="@+id/adView"
android:layout_width="350dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
ads:adUnitId=".............."
ads:adSize="BANNER"
ads:testDevices="TEST_EMULATOR, TEST_DEVICE_ID"
ads:loadAdOnCreate="true"/>
我注意到有些人说我已经将变量设置为null,它不应该是,但我想我没有这样做。我真的在努力解决我现在要做的事情,我一直在搜索google和SO以寻找与此主题相关的其他问题/帖子,但我还没有找到解决方案......