Android:Intent- stopps Actiity

时间:2017-03-12 13:51:03

标签: java android android-intent

我在Android Studio中编写了一个简单的游戏。比赛结束后,我想切换到另一个活动,但意图不起作用。每次应用程序发送错误" unfortunalety停止"。当图片"捐赠时,我在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;

    }


}


}

` 这些是我的Logcats

  

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)                                                                                                  at 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)                                                                                                  在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)                                                                                                  在android.app.Activity.dispatchTouchEvent(Activity.java:2454)                                                                                                  在com.android.internal.policy.impl.PhoneWindow $ DecorView.dispatchTouchEvent(PhoneWindow.java:2000)                                                                                                  在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)                                                                                                  在android.os.MessageQueue.nativePollOnce(本机方法)                                                                                                  在android.os.MessageQueue.next(MessageQueue.java:131)                                                                                                  在android.os.Looper.loop(Looper.java:132)                                                                                                  在android.app.ActivityThread.main(ActivityThread.java:5511)                                                                                                  at java.lang.reflect.Method.invokeNative(Native Method)                                                                                                  在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)                                                                                                  at dalvik.system.NativeStart.main(Native Method)   03-12 14:42:22.043 16202-16202 / com.example.kerstin.dojozusammenfassung W / dalvikvm:threadid = 1:线程退出时未捕获异常(group = 0x40d0b390)   03-12 14:42:22.063 16202-16202 / com.example.kerstin.dojozusammenfassung E / AndroidRuntime:FATAL EXCEPTION:main                                                                                            显示java.lang.NullPointerException                                                                                                在com.example.kerstin.dojozusammenfassung.GamePlayScene.receiveTouch(GamePlayScene.java:202)                                                                                                at 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)                                                                                                在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)                                                                                                在android.app.Activity.dispatchTouchEvent(Activity.java:2454)                                                                                                在com.android.internal.policy.impl.PhoneWindow $ DecorView.dispatchTouchEvent(PhoneWindow.java:2000)                                                                                                在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)                                                                                                在android.os.MessageQueue.nativePollOnce(本机方法)                                                                                                在android.os.MessageQueue.next(MessageQueue.java:131)                                                                                                在android.os.Looper.loop(Looper.java:132)                                                                                                在android.app.ActivityThread.main(ActivityThread.java:5511)                                                                                                at java.lang.reflect.Method.invokeNative(Native Method)                                                                                                在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(本地方法)

清单:`

<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>

`

2 个答案:

答案 0 :(得分:0)

在android中你通常在onCreate()方法中做的事情不在构造函数中。 尝试在onCreate()中调用GameplayScene()(假设它是你想做的第一件事)或将其代码放在onCreate()方法中。您的TouchEvent()可能返回null,因为没有任何内容被初始化,因为它不在onCreate()中。

答案 1 :(得分:0)

我发现了错误。 GameplayScene不是一个令人讨厌的场景,我没有将上下文转移到GameplaScene,因此缺少上下文。