【问题标题】:Transferring int array through intent-Android通过intent-Android传输int数组
【发布时间】:2015-02-16 13:09:45
【问题描述】:

我正在尝试通过 Intent 将一个 int 数组传递给 Android 中的另一个 Activity

在第一方面我有:

    Intent intent = new Intent(this,StatsPage.class);
    intent.putExtra(STATISTICS,NUMBERS_ROLLED);
    startActivity(intent);

另一方面,我有:

    Bundle extras = getIntent().getExtras();
    int[] arrayOfNums = extras.getIntArray("STATISTICS");

这段代码总是让我崩溃。
有什么建议么?谢谢

【问题讨论】:

  • 有什么异常?
  • STATISTICS 的类型是什么?

标签: android arrays android-intent


【解决方案1】:

你需要把你的额外内容放在下面

Intent intent = new Intent(this,StatsPage.class);
intent.putExtra("STATISTICS", NUMBERS_ROLLED);
startActivity(intent);

【讨论】:

    【解决方案2】:

    在您的源活动中,确保您的密钥 STATISTICS 定义为 public static final常量。在您的目标活动中,您可以将其称为

    Bundle extras = getIntent().getExtras();
    int[] arrayOfNums = extras.getIntArray(SourceActivity.STATISTICS);
    

    这可确保您不会因为键名不匹配而无法找到额外的键并遇到NullPointerException

    【讨论】:

      【解决方案3】:

      就这样写吧:

      Bundle extras = getIntent().getExtras();
      int[] arrayB = extras.getIntArray(SourceActivity.STATISTICS);
      

      【讨论】:

        猜你喜欢
        • 2023-03-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多