stopps Actiity

I programmed an easy Game in Android Studio. After game Over I want to switch to another Activity, but Intent doesn't work. Every time the app sends the error "unfortunalety stopped". I call the Intent in the onTouch Method when the Picture "donate" is pressed. I tried various combinations with context or with getApplicationContext() and so on. I surfed through various platforms, but I couln't fix the problem!

Thanks for your help!

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

    }


}


}

` These are my Logcats

03-12 14:42:22.033 16202-16202/com.example.kerstin.dojozusammenfassung E/InputEventReceiver: Exception dispatching input event. 03-12 14:42:22.033 16202-16202/com.example.kerstin.dojozusammenfassung E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback 03-12 14:42:22.043 16202-16202/com.example.kerstin.dojozusammenfassung E/MessageQueue-JNI: java.lang.NullPointerException at com.example.kerstin.dojozusammenfassung.GamePlayScene.receiveTouch(GamePlayScene.java:202) at com.example.kerstin.dojozusammenfassung.SceneManager.receiveTouch(SceneManager.java:21) at com.example.kerstin.dojozusammenfassung.GamePanel.onTouchEvent(GamePanel.java:63) at android.view.View.dispatchTouchEvent(View.java:7198) 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.dispatchTouchEvent(ViewGroup.java:1888) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2187) at android.view.ViewGroup.dispatchTouchE vent(ViewGroup.java:1888) at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2052) at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java: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) at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3539) at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3468) at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4577) at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4556) at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4652) at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:184) at android.os.MessageQueue.nativePollOnce(Native Method) at android.os.MessageQueue.next(M essageQueue.java:131) at 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) at dalvik.system.NativeStart.main(Native Method) 03-12 14:42:22.043 16202-16202/com.example.kerstin.dojozusammenfassung W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x40d0b390) 03-12 14:42:22.063 16202-16202/com.example.kerstin.dojozusammenfassung E/AndroidRuntime: FATAL EXCEPTION: main java.lang.NullPointerException at com.example.kerstin.dojozusammenfassung.GamePlayScene.receiveTouch(GamePlayScene.java:202) at com.example.kerstin.dojozusammenfassung.SceneManager.receiveTouch(SceneManager.java:21) at com.example.kerstin.dojozusammenfassung.GamePanel.onTouchEvent(GamePanel.java:63) at a ndroid.view.View.dispatchTouchEvent(View.java:7198) 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.dispatchTouchEvent(ViewGroup.java:1888) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2187) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1888) at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2052) at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java: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) at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3539) at android.view.ViewRootI mpl.deliverInputEvent(ViewRootImpl.java:3468) at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4577) at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4556) at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4652) at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:184) at android.os.MessageQueue.nativePollOnce(Native Method) at android.os.MessageQueue.next(MessageQueue.java:131) at 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) at dalvik.system.NativeStart.main(Native Method)

Manifest : `

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

`


Well in android you generally do things in onCreate() Method not in a constructor. Try calling GameplayScene() (Assuming if its the first thing you want to do) in onCreate() or place its code in the onCreate() Method. Your TouchEvent() is probably returning null because nothing gets initialized as it's not in onCreate().


I found out the error. GameplayScene wasn't the aktive scene and I didn't transfered the context to the GameplaScene, so the context was missing.

链接地址: http://www.djcxy.com/p/18362.html

上一篇: ActiveMQ或RabbitMQ或ZeroMQ或

下一篇: 停止活动