简体   繁体   中英

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. Now i want to replay the level-1. For this i have used getExtra(). My app crashes when i click the replay button.

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);
            }

        }
    });
}

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. 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. Your app will go back to the Activity1 or 2 whoever call it. 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. 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. It is explain here check the accepted answer. How to manage `startActivityForResult` on Android?

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. Because of this, the build will never be successful.

Also, when you declare intents, you MUST pass on the activity_name.class file.

Something you can try:

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

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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