java - 从 Activity 1 切换到 Activity 2 时应用崩溃

标签 java android

在我的应用程序中,有很多被称为关卡的 Activity 。一项 Activity 是奖励 Activity 。当我赢得一级时,奖励 Activity 开始。现在我想重播 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);
    }

}

奖励.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);
            }

        }
    });
}

最佳答案

使用您发布的 java 代码,在 Reward.java 文件中,您试图创建另一个 Intent 对象,其名称与其上方范围内声明的对象同名。因此,构建永远不会成功。

此外,当您声明 Intent 时,您必须传递 activity_name.class 文件。

你可以尝试的东西:

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) 奖励.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);
            }
        }
    });
}

此外,如果您只是简单地使用 Reward.java 文件来获取之前的 Intent 数据,执行一些计算,并将一些数据发送回调用或父 Activity ,那么您可以只需使用 startActivityForResult() 方法,它会处理您尝试手动执行的操作。

这里有一篇小文章,也许能帮助你解决这个问题

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

关于java - 从 Activity 1 切换到 Activity 2 时应用崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51340175/

相关文章:

Java 列表与数组[]。以前的线程说使用列表,我不相信。

java - 按值更改 jTable 单元格颜色

java - Android 异步任务已弃用。需要替代示例

android - 需要什么 USE_BIOMETRIC 权限?

android - 在 ImageView 的 OnTouch 中返回 false 但事件仍在消耗

java - 使用自定义流媒体应用程序在 Wowza 上使用 PlayReady DRM

java - 等待ParallelFlux完成

android - 扩展 Activity 的标题未出现自定义样式

Android 导航栏和状态栏高程/阴影

android - 谷歌分析 android 的自动 Activity 跟踪如何工作