从活动1切换到活动2时,应用崩溃

时间:2018-07-14 13:57:37

标签: java android

在我的应用中,有很多活动被称为关卡。一种活动是奖励活动。当我赢得1级时,奖励活动开始。现在我想重播1级。为此,我使用了getExtra()。单击重播按钮时,我的应用程序崩溃。

Houselevel1.java

 public void getReward(){
    if(count == 3) {
        Intent intent = new Intent("com.creatives.arfa.revealthesecretsgame.Reward");
        intent.putExtra("activity", "level1");
        startActivity(intent);

    }

}

HouseLevel2.java

    public void getReward(){
    if(count == 3) {
        Intent intent = new Intent("com.creatives.arfa.revealthesecretsgame.Reward");
        intent.putExtra("activity", "level2");
        startActivity(intent);
    }

}

Reward.java

  public void replayLevel() {
    replay = (ImageButton) findViewById(R.id.replay);
    Intent intent= getIntent();
    activity = intent.getStringExtra("activity");
    replay.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View paramView) {
            if(activity.equals("level2")){
                Intent intent = new Intent("com.creatives.arfa.revealthesecretsgame.HouseLevel2");
                startActivity(intent);
            }

            if(activity.equals("level1")){
                Intent intent = new Intent("com.creatives.arfa.revealthesecretsgame.Houselevel1");
                startActivity(intent);
            }

        }
    });
}

2 个答案:

答案 0 :(得分:0)

如果您只想从活动1或活动2转到“奖励”活动,则抢些东西并将其发送回任一活动。 您要做的是 startActivityForResult 。您传递一个ID(常数)来执行您对“奖励”活动的操作,将需要返回的内容打包到捆绑中,然后将ActivtyResult设置为“确定”并关闭活动。 您的应用将返回给Activity1或2,无论谁调用它。在这些活动上,您将重写onActivityResult方法。在此,您将检查结果的来源ID是否是您在startActivityForResult上发送的ID,以及状态是否正常。 然后,您就可以对“奖励”活动进行任何设置。奖励活动无需知道它来自何处,只要能获取一些数据即可。因此,您以后可以拥有一个调用Reward活动的Activity3,而无需修改Reward活动。 在这里说明检查接受的答案。 How to manage `startActivityForResult` on Android?

答案 1 :(得分:0)

使用您发布的Java代码,在public void run(){ //Do some long running task once done isReady.set(true); } 文件中,您试图创建另一个Intent Object,其名称与在其上方的作用域中声明的名称相同。因此,构建将永远不会成功。

此外,当您声明意图时,必须传递Reward.java文件。

您可以尝试的方法:

1)HouseLevel1.java

activity_name.class

2)HouseLevel2.java

public void getReward(){
    if(count == 3) {
        Intent intent = new Intent(getApplicationContext(), com.creatives.arfa.revealthesecretsgame.Reward.class);
        intent.putExtra("activity", "level1");
        startActivity(intent);

    }
}

3)Reward.java

public void getReward(){
    if(count == 3) {
        Intent intent = new Intent(getApplicationContext(), com.creatives.arfa.revealthesecretsgame.Reward.class);
        intent.putExtra("activity", "level2");
        startActivity(intent);

    }
}

此外,如果您只是使用public void replayLevel() { replay = (ImageButton) findViewById(R.id.replay); Intent intent= getIntent(); activity = intent.getStringExtra("activity"); replay.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View paramView) { if(activity.equals("level2")){ Intent intent = new Intent(getApplicationContext(), com.creatives.arfa.revealthesecretsgame.HouseLevel2.class); startActivity(intent); } else if(activity.equals("level1")){ Intent intent = new Intent(getApplicationContext(), com.creatives.arfa.revealthesecretsgame.Houselevel1.class); startActivity(intent); } } }); } 文件来获取先前意图的数据,执行一些计算并将一些数据发送回调用或父活动,那么您可以简单地使用{{ 1}}方法,它会注意您要手动执行的操作。

这是一篇小文章,也许可以帮助您解决问题

  

http://www.vogella.com/tutorials/AndroidIntent/article.html#retrieving-result-data-from-a-sub-activity

相关问题