尝试在Android Studios上启动我的第三项活动时,将其重定向到初始活动

时间:2018-10-05 18:03:26

标签: java android android-intent android-activity

我正在尝试创建一个应用,在该应用中我从第二个活动中启动了第三个Activity,而第二个活动又是由第一个Activity启动的。每当我尝试启动第三个Activity时,应用程序都会暂停一秒钟,然后恢复初始活动而不会崩溃

为了帮助您,我在下面发布了我的代码!

int ranNum = (int)(Math.random()*playerNames.size());
        String location = res.getStringArray(R.array.location)[(int)(Math.random()*res.getStringArray(R.array.location).length)];
        time = Integer.getInteger(timeSelection.getSelectedItem().toString());
        Intent gamePrep = new Intent(this, GamePrep.class);
        gamePrep.putExtra("time", time);
        gamePrep.putExtra("playerNames", playerNames.toArray());
        gamePrep.putExtra("numPlayers",numPlayers);
        gamePrep.putExtra("spyPlayer",playerNames.get(ranNum));
        gamePrep.putExtra("location", location);
        gamePrep.putExtra("roleList", locationRoles.get(location));
        gamePrep.putExtra("curPlayerNum", 0);
        startActivityForResult(gamePrep,1);

编辑:这是GamePrep onCreate()代码

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_game_prep);
    curPlayerNum = getIntent().getIntExtra("curPlayerNum", 0);
    numPlayers = getIntent().getIntExtra("numPlayers", 0);
    time = getIntent().getIntExtra("time", 5);
    playerNames = getIntent().getStringArrayExtra("playerNames");
    spyPlayer = getIntent().getStringExtra("spyPlayer");
    location = getIntent().getStringExtra("location");
    roleList = getIntent().getStringArrayExtra("roleList");
    curPlayer = playerNames[curPlayerNum];
    isSpy = curPlayer.equals(spyPlayer);
    next = findViewById(R.id.revealButton);
    nameOutput = findViewById(R.id.nameOutput);
    locationOutput = findViewById(R.id.locationOutput);
    roleOutput = findViewById(R.id.roleOutput);
    res = getResources();
    next.setText(res.getText(R.string.reveal));
    nameOutput.setText(curPlayer);
    next.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (curPlayerNum + 1 != playerNames.length) {
                if (!revealed) {
                    reveal();
                    revealed = true;
                    if(curPlayerNum+1 != playerNames.length) {
                        next.setText(res.getString(R.string.nextPlayer));
                    }
                    else
                    {
                        next.setText(res.getString(R.string.begin));
                    }
                }
                else
                    {
                    nextPlayer();
                    revealed = false;
                    next.setText(res.getString(R.string.reveal));
                }
            }
            else
            {
                startGame();
            }
        }
    });
}

第一个类onCreate()

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_start_screen);
    openIntent = findViewById(R.id.startButton);
    openIntent.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(StartScreen.this, GameMenu.class);
            startActivityForResult(intent,0);
        }
    });
}

我没有任何一种活动的简历方法。 再次感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

请发布您的第一个Activity的代码,我认为问题出在onCreate()或onResume()方法上。

但是,您可以执行许多操作,例如使用参数playerNames.size()随机查找数字。所有这些行都可能花费很多时间(平均可能需要1秒钟)。