“key”的值为null

时间:2016-08-06 19:34:53

标签: android android-intent

我有这个问题:

我想在不同的活动中使用变量,但我的键值为null。

我在主要活动上写了这篇文章:

    Intent intent = new Intent(MainActivity.this, Main2Activity.class);
    intent.putExtra("score",score);
    startActivity(intent);
}

我在第二次活动中写道:

Bundle extras = getIntent().getExtras();
score10 = extras.getString("score");
score20 = Integer.parseInt(score10);
score30 = score20;

我该怎么办?

3 个答案:

答案 0 :(得分:0)

MainActivity.Java

Bundle extras = getIntent().getExtras();
score10 = extras.getString("score");
if(score10 != null || score10 != ""){
    score20 = Integer.parseInt(score10);
    score30 = score20;
}

MainActivity2.java

#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>

static int lock;

void f(int);

int main(int ac, char *av[])
{

  if (ac == 1) {
    printf("No argument provided");
    exit(1);
  }

  int i = atoi(av[1]);
  printf("This is argument av: %i \n", i);
  signal(SIGINT, f);
  int k = 0;
  while (k != i) {
    printf("help %d \n", k);
    while (lock == 1) {
      puts("Sleeping, waiting for signal");
      sleep(1);
    }
    sleep(1);
    k++;
  }
  return 0;
}

void f(int signum)
{
  signal(SIGINT, f);
  printf("Signal raised %d\n", signum);
  if (lock == 1) {
    lock = 0;
  } else {
    lock = 1;
  }
}

已经完成了。

答案 1 :(得分:0)

就像你提到的scoreint一样。试试这个:

Intent extras = getIntent();
int score10 = extras.getIntExtra("score", 0); 
score30=score10;
// Whatever other allocations you need

答案 2 :(得分:0)

使用

 extras.getIntExtra("score",0);

而不是

  extras.getString("score");