Android:你如何通过点击进入另一个活动? [英] Android: How do you go to another activity on click?

查看:45
本文介绍了Android:你如何通过点击进入另一个活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试依次进行第三个活动.从主要活动转到第二个活动很好,但是当我尝试从第二个活动转到第三个活动时,应用程序崩溃了.

I'm trying to move onto the third activity in a sequence. Going from the main activity to the second one works fine but when I try to go to the third activity from the second I the application crashes.

这是我的第二个活动的代码:

Here's my code for the second activity:

package com.example.helloandroid;

import android.app.Activity;
//other imports here

public class Game extends Activity implements OnClickListener {

    private static final String TAG = "Matrix";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.matrix);
        View doneButton = findViewById(R.id.done_button);
        doneButton.setOnClickListener(this);
    }

    public void onClick(View v) { 
        switch (v.getId()) { 
            case R.id.done_button:
                Intent k = new Intent(this, GameTwo.class);
                startActivity(k);
                //finish();
                break;
        }
    }
}

以及第三个活动的代码:

And the code for the third activity:

package com.example.helloandroid;

import android.app.Activity;
//other imports here

public class GameTwo extends Activity {

   private static final String TAG = "Matrix";

   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       this.setContentView(R.layout.matrixtwo);
       View donetwoButton = findViewById(R.id.donetwo_button);
   }
}

推荐答案

switch中试试下面的代码:

try {
    Intent k = new Intent(Game.this, GameTwo.class);
    startActivity(k);
} catch(Exception e) {
    e.printStackTrace();
}

告诉我这有用吗.....

Tell me is this helpful.....

这篇关于Android:你如何通过点击进入另一个活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆