停止活动
我在Android Studio中编写了一个简单的游戏。 游戏结束后,我想切换到另一个活动,但意图不起作用。 每当应用程序发送错误“不幸停止”。 当按下图片“捐赠”时,我在onTouch方法中调用Intent。 我尝试了使用上下文或getApplicationContext()等的各种组合。 我通过各种平台冲浪,但我没有解决问题!
谢谢你的帮助!
活动GamePlayScene
public class GamePlayScene extends Activity implements Scene {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public GamePlayScene()
{
player = new RectPlayer(new Rect(100, 100, 200, 200), Color.rgb(255, 0, 0));
playerPoint = new Point(Constants.SCREEN_WITH / 2, 7 * Constants.SCREEN_HEIGHT / 8); //Constants.SCREEN_WITH/2, 7*Constants.SCREEN_HEIGHT/8
player.update(playerPoint);
obstacleManager = new ObstacleManager(100, Constants.SCREEN_HEIGHT / 3, 100, Color.YELLOW);
coinManager = new CoinManager(79, Constants.SCREEN_HEIGHT, 76, Color.YELLOW);
gameState = 0;
orientationData = new OrientationData();
orientationData.register();
frameTime = System.currentTimeMillis();
up = false;
wait = true;
BitmapFactory bf = new BitmapFactory();
restartButtonBitmap = bf.decodeResource(Constants.CURRENT_CONTEXT.getResources(), R.drawable.restartbutton);
donateButtonBitmap = bf.decodeResource(Constants.CURRENT_CONTEXT.getResources(), R.drawable.donate);
}
public void reset() {
playerPoint = new Point(Constants.SCREEN_WITH / 2, 7 * Constants.SCREEN_HEIGHT / 8);
player.update(playerPoint);
obstacleManager = new ObstacleManager(100, 2 * Constants.SCREEN_HEIGHT / 10, 100, Color.BLUE);
coinManager = new CoinManager(79, Constants.SCREEN_HEIGHT, 76, Color.YELLOW);
movingPlayer = false;
konto = konto + muenzen;
reset = true;
speed = false;
gameState = 0;
muenzen = 0;
count = 0;
}
@Override
public void receiveTouch(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
float posX = event.getX();
float posY = event.getY();
float x1 = (Constants.SCREEN_WITH / 2) - (restartButtonBitmap.getWidth() / 2),
x2 = Constants.SCREEN_WITH / 2 + (restartButtonBitmap.getWidth() / 2),
y1 = Constants.SCREEN_HEIGHT / 2 - (restartButtonBitmap.getHeight() / 2),
y2 = Constants.SCREEN_HEIGHT / 2 + (restartButtonBitmap.getHeight() / 2);
float x3 = (Constants.SCREEN_WITH / 2) - (donateButtonBitmap.getWidth() / 2),
x4 = Constants.SCREEN_WITH / 2 + (donateButtonBitmap.getWidth() / 2),
y3 = Constants.SCREEN_HEIGHT / 2 + restartButtonBitmap.getHeight() / 2 + 50 + donateButtonBitmap.getHeight() / 2,
y4 = Constants.SCREEN_HEIGHT / 2 + restartButtonBitmap.getHeight() / 2 + 50 + donateButtonBitmap.getHeight() / 2 +
donateButtonBitmap.getHeight();
if (gameState == 0) {
gameState++;
}
if (gameOver && (posX >= x1 && posX <= x2) && (posY >= y1 && posY <= y2)) {
// we are in the square
gameState = 2;
//http://androidforums.com/threads/touch-event-on-image-made-on-canvas.210019/
}
if (!gameOver && gameState == 1) {
movingPlayer = true;
count++;
start=true;
if(count==1)
{
jump();
}
}
if (gameOver && (posX >= x3 && posX <= x4) && (posY >= y3 && posY <= y4)) {
Intent intent = new Intent(GamePlayScene.this, UserLogin.class);
startActivity(intent);
}
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
movingPlayer = false;
up = false;
break;
}
}
@Override
public void draw(Canvas canvas) {
canvas.drawColor(Color.WHITE);
player.draw(canvas);
obstacleManager.draw(canvas);
coinManager.draw(canvas);
if (gameOver) {
canvas.drawBitmap(restartButtonBitmap, Constants.SCREEN_WITH / 2 - restartButtonBitmap.getWidth() / 2,
Constants.SCREEN_HEIGHT / 2 - restartButtonBitmap.getHeight() / 2, new Paint());
canvas.drawBitmap(donateButtonBitmap, Constants.SCREEN_WITH / 2 - donateButtonBitmap.getWidth() / 2,
Constants.SCREEN_HEIGHT / 2 + restartButtonBitmap.getHeight() / 2 + 50 + donateButtonBitmap.getHeight() / 2, new Paint());
}
}
@Override
public void update() {
if (playerPoint.y > Constants.SCREEN_HEIGHT)
{
gameOver = true;
}
if (gameState == 2) {
reset();
gameState = 0;
orientationData.newGame();
gameOver = false;
}
}
}
`这是我的日志
03-12 14:42:22.033 16202-16202 / com.example.kerstin.dojozusammenfassung E / InputEventReceiver:异常调度输入事件。 03-12 14:42:22.033 16202-16202 / com.example.kerstin.dojozusammenfassung E /的MessageQueue-JNI:异常中的MessageQueue回调:handleReceiveCallback 03-12 14:42:22.043 16202-16202 / com.example.kerstin.dojozusammenfassung E /的MessageQueue-JNI:显示java.lang.NullPointerException在com.example.kerstin.dojozusammenfassung.GamePlayScene.receiveTouch(GamePlayScene.java:202)在com.example.kerstin.dojozusammenfassung.SceneManager.receiveTouch(SceneManager.java:21)在com.example.kerstin.dojozusammenfassung.GamePanel.onTouchEvent(GamePanel.java:63)在android.view.View.dispatchTouchEvent(View.java:7198)在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2187)在安卓.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1888)at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2187)at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1888)at android.view.ViewGroup。 dispatchTransformedTouchEvent(ViewGroup.java:2187)at android.view.ViewGroup.dispatchTouchE 在com.android.internal.policy.impl.PhoneWindow $ DecorView.superDispatchTouchEvent(PhoneWindow.java:2052)在com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java发泄(ViewGroup.java:1888): 1506)at android.app.Activity.dispatchTouchEvent(Activity.java:2454)at com.android.internal.policy.impl.PhoneWindow $ DecorView.dispatchTouchEvent(PhoneWindow.java:2000)at android.view.View.dispatchPointerEvent(View的.java:7392)在android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3539)在android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3468)在android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4577)在android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4556)在android.view.ViewRootImpl $ WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4652)在android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:184)的机器人。 os.MessageQueue.nativePollOnce(Native Method)在android.os.MessageQueue.next(M android.app.ActivityThread.main(ActivityThread.java:5511)上的android.os.Looper.loop(Looper.java:132)上的java.lang.reflect.Method.invokeNative(Native Method)上的essageQueue.java:131)在java.lang.reflect.Method.invoke(Method.java:511)在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1029)在com.android.internal.os.ZygoteInit.main (ZygoteInit.java:796)在dalvik.system.NativeStart.main(本机方法)03-12 14:42:22.043 16202-16202 / com.example.kerstin.dojozusammenfassung W / dalvikvm:线程ID = 1:未捕获与螺纹离开异常(组= 0x40d0b390)03-12 14:42:22.063 16202-16202 / com.example.kerstin.dojozusammenfassung E / AndroidRuntime:致命异常:在主com.example.kerstin.dojozusammenfassung.GamePlayScene.receiveTouch显示java.lang.NullPointerException (GamePlayScene.java:202)在com.example.kerstin.dojozusammenfassung.SceneManager.receiveTouch(SceneManager.java:21)在com.example.kerstin.dojozusammenfassung.GamePanel.onTouchEvent(GamePanel.java:63)在一个 ndroid.view.View.dispatchTouchEvent(View.java:7198)在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2187)在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1888)在android.view.ViewGroup .dispatchTransformedTouchEvent(ViewGroup.java:2187)在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1888)在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2187)在android.view.ViewGroup.dispatchTouchEvent(ViewGroup中。 Java的:1888)在com.android.internal.policy.impl.PhoneWindow $ DecorView.superDispatchTouchEvent(PhoneWindow.java:2052)在com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1506)在安卓.app.Activity.dispatchTouchEvent(Activity.java:2454)at com.android.internal.policy.impl.PhoneWindow $ DecorView.dispatchTouchEvent(PhoneWindow.java:2000)at android.view.View.dispatchPointerEvent(View.java:7392 )在android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3539)在android.view.ViewRootI mpl.deliverInputEvent(ViewRootImpl.java:3468)在android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4577)在android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4556)在android.view.ViewRootImpl $ WindowInputEventReceiver.onInputEvent (ViewRootImpl.java:4652)在android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:184)在android.os.MessageQueue.nativePollOnce(本机方法)在android.os.MessageQueue.next(MessageQueue.java:131)在android.os.Looper.loop(Looper.java:132)at android.app.ActivityThread.main(ActivityThread.java:5511)at java.lang.reflect.Method.invokeNative(Native Method)at java.lang.reflect。 Method.invoke(Method.java:511)at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1029)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)在dalvik.system.NativeStart.main(本地方法)
清单:`
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@drawable/unnamed"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".Info" />
<activity android:name=".Einstellungen" />
<activity android:name=".Spenden" />
<activity android:name=".Spenden2" />
<activity android:name=".MainGame" />
<activity android:name=".LoginActivity" />
<activity android:name=".RegisterActivity" />
<activity android:name=".UserLogin" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".GamePlayScene"></activity>
</application>
`
那么在android中,你通常在onCreate()方法中做的事情不在构造函数中。 尝试在onCreate()中调用GameplayScene()(假设它是您想要做的第一件事),或将其代码置于onCreate()方法中。 你的TouchEvent()可能返回null,因为没有被初始化,因为它不在onCreate()中。
我发现了这个错误。 GameplayScene不是动画场景,我没有将上下文转移到GameplaScene,所以上下文不见了。
链接地址: http://www.djcxy.com/p/18361.html上一篇: stopps Actiity