简体   繁体   English

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

[英]App crashes when switching from activity 1 to activity 2

In my app, there are so many activities which are referred to as levels. 在我的应用程序中,有很多活动被称为关卡。 And one activity is Reward activity. 一种活动是奖励活动。 when i win level-1, reward activity opens. 当我赢得1级时,奖励活动开始。 Now i want to replay the level-1. 现在我想重播1级。 For this i have used getExtra(). 为此,我使用了getExtra()。 My app crashes when i click the replay button. 单击重播按钮时,我的应用程序崩溃。

Houselevel1.java 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 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 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);
            }

        }
    });
}

If all you want is go from Activity 1 or to 2 to a Reward activity grab something and send that something back to either activity. 如果您只想从活动1或活动2转到“奖励”活动,则抢些东西并将其发送回任一活动。 What you do is startActivityForResult You pass an Id (constant number) do what you do on the Reward activty, pack what you need to return in a Bundle, and set ActivtyResult to OK and close your activity. 您要做的是startActivityForResult,您传递一个ID(常数)以执行对Reward活动的操作,将需要返回的内容打包到Bundle中,并将ActivtyResult设置为OK,然后关闭活动。 Your app will go back to the Activity1 or 2 whoever call it. 您的应用将返回给Activity1或2,无论谁调用它。 On those activties you override the method onActivityResult There you check if the id on which the result is coming from is the Id you sent on the startActivityForResult and if the status is OK. 在这些活动上,您将重写onActivityResult方法。在此,您将检查结果的来源ID是否是您在startActivityForResult上发送的ID,以及状态是否正常。 Then you have whatever was set on the Reward activity. 然后,您就可以对“奖励”活动进行任何设置。 The Reward activity don't need to know from where it came from if only will grab some data. 奖励活动无需知道它来自何处,只要能获取一些数据即可。 So you can later have an Activity3 that calls the Reward activity and you do not need to modify the Reward activity. 因此,您以后可以拥有一个调用Reward活动的Activity3,而无需修改Reward活动。 It is explain here check the accepted answer. 在这里说明检查接受的答案。 How to manage `startActivityForResult` on Android? 如何在Android上管理`startActivityForResult`?

With the java code you've posted, in the Reward.java file, you're trying to create another Intent Object with the same name as the one declared in the scope right above it. 使用您发布的Java代码,在Reward.java文件中,尝试创建另一个Intent Object,其名称与在其上方的作用域中声明的名称相同。 Because of this, the build will never be successful. 因此,构建将永远不会成功。

Also, when you declare intents, you MUST pass on the activity_name.class file. 另外,在声明意图时,必须传递activity_name.class文件。

Something you can try: 您可以尝试以下操作:

1) HouseLevel1.java 1)HouseLevel1.java

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

    }
}

2) HouseLevel2.java 2)HouseLevel2.java

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

    }
}

3) Reward.java 3)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(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);
            }
        }
    });
}

Also, if you're simply using the Reward.java file to get the previous intent's data, perform some calculation, and send some data back to the calling, or parent activity, then you can simply use the startActivityForResult() method, which takes care what what you're trying to do manually. 另外,如果您只是使用Reward.java文件获取先前意图的数据,执行一些计算并将一些数据发送回调用或父活动,则可以简单地使用startActivityForResult()方法,该方法需要关心您要手动执行的操作。

Here's a small article that might be able to help you with the problem 这是一篇小文章,可能会帮助您解决问题

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

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

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