简体   繁体   English

尝试切换到其他活动时,Android应用程序崩溃

[英]Android app crashes upon attempt to switch to a different Activity

I am running into an issue in my app. 我的应用程序出现问题。 When my game ends (when life == 0 ) I am attempting to switch to a game over screen by using a different activity. 当我的游戏结束时(当life == 0 ),我试图通过使用其他活动通过屏幕切换到游戏。 When the game ends, the app simply crashes. 游戏结束后,应用程序便崩溃了。 I have included the XML for the activity I am trying to switch from as well as indicating where the app crashes. 我已经包含了我要从中切换的活动的XML,并指出了应用程序崩溃的位置。 If anyone could help out, that would be great! 如果有人可以帮忙,那就太好了! Thanks. 谢谢。

activity_game.XML: activity_game.XML:

在此处输入图片说明

SurfaceView I am trying to switch from once game ends: 我试图从游戏结束后切换到SurfaceView

public class SVGameView extends SurfaceView implements Runnable {
        private SurfaceHolder holder;
    Thread thread = null;
    volatile boolean running = false;
    static final long FPS = 30;
    private Sprite sprite;
    private long lastClick;

    private Bitmap ball, gameOver;
    //private int x = 200, y = 200;
    private int scorePosX = 100;
    private int scorePosY = 100;
    private int countScore = 0;
    private int life = 1;

    public SVGameView(Context context) {
        super(context);
        thread = new Thread(this);
        holder = getHolder();
        holder.addCallback(new SurfaceHolder.Callback() {

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
                boolean retry = true;
                running = false;
                while (retry) {
                    try {
                        thread.join();
                        retry = false;
                    } catch (InterruptedException e) {
                    }
                }
            }

            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                running = true;
                thread.start();
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format,
                                       int width, int height) {
            }
        });

        ball = BitmapFactory.decodeResource(getResources(), R.drawable.ball2);
        gameOver = BitmapFactory.decodeResource(getResources(),R.drawable.endscreen);
        sprite = new Sprite(this, ball);
    }

    @Override
    public void run() {
        long ticksPS = 1000 / FPS;
        long startTime;
        long sleepTime;

        while (running) {
            Canvas c = null;
            startTime = System.currentTimeMillis();
            try {
                c = getHolder().lockCanvas();
                synchronized (getHolder()) {
                    update();
                    draw(c);
                }
            } finally {
                if (c != null) {
                    getHolder().unlockCanvasAndPost(c);
                }
            }
            sleepTime = ticksPS-(System.currentTimeMillis() - startTime);
            try {
                if (sleepTime > 0)
                    thread.sleep(sleepTime);
                else
                    thread.sleep(10);
            } catch (Exception e) {}

        }
    }

    private void update(){
        sprite.update();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawColor(Color.WHITE);
        Paint paint = new Paint();
        canvas.drawPaint(paint);
        paint.setColor(Color.WHITE);
        paint.setTextSize(48);
        canvas.drawText("Score: " + countScore, scorePosX, scorePosY, paint);
        canvas.drawText("Lives: " + life, 500, 100, paint);
        sprite.onDraw(canvas);

        //Crashes here
        if(life == 0) {
            getContext().startActivity(new Intent(getContext(), SVGameOver.class));
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        if(System.currentTimeMillis()-lastClick > 300){
            lastClick = System.currentTimeMillis();
        }

        synchronized (getHolder()){
            if(sprite.isHit(event.getX(), event.getY())){
                countScore += 1;
                sprite.increase();
            }else{
                life --;
            }
        }
        return super.onTouchEvent(event);
    }
}

Activity I am trying to reach once the game ends: 游戏结束后,我尝试达到的活动:

public class SVGameOver extends Activity {

    private Bitmap gameOverScreen;

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game);

        gameOverScreen = BitmapFactory.decodeResource(getResources(), R.drawable.endscreen);
    }

    protected void onDraw(Canvas canvas){
        canvas.drawBitmap(gameOverScreen, 0,0,null);
    }
}

I think logcat is asking you the right question: "have you declared this activity in your AndroidManifest.xml" ? 我认为logcat在问您一个正确的问题:“您是否已在AndroidManifest.xml中声明此活动”?

If you think you did it, It's highly probable you did it in a wrong way, most of the times that you think you added an Activity to the manifest but you are receiving this kind of crash, 99,9% of the time you declared it with a wrong namespace 如果您认为自己做了错,很可能是您以错误的方式做了,在大多数情况下,您认为您已向清单中添加了活动,但您收到此类崩溃的概率是99.9%它具有错误的名称空间

Declare SVGameOver activity in your AndroidManifest.xml: 在AndroidManifest.xml中声明SVGameOver活动:

<activity
    android:name="com.example.welcome.assignment2.SVGameOver">
    ...
</activity>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM