getIntent.getExtras.getString()中的null值

gho*_*der 3 android android-intent

这是我第一次活动中的代码:

Intent i = new Intent(this, OtherScreen.class);
    i.putExtra("id1", "first");
    i.putExtra("id2", "second");
    startActivity(i);
Run Code Online (Sandbox Code Playgroud)

第一个,第二个是我想要传递的值.在我的另一堂课上我有这个:

Intent i = getIntent();
        Bundle extras = i.getExtras();
        String result = extras.getString("id1");
        System.out.println("yeah"+result);
Run Code Online (Sandbox Code Playgroud)

但是在我运行之后,我的结果是空的.你能帮助我吗?如果我以这种方式编写我的getString,我会收到语法错误.

String result = extras.getString(id1); //id1 cannot be resolved to a variable
String result = extras.getString("id1","default value"); // remove argument
Run Code Online (Sandbox Code Playgroud)

Apr*_*ith 8

Intent i = new Intent(yourcurrentactivity.this, OtherScreen.class);

i.putExtra("id1", "first");
i.putExtra("id2", "second");
startActivity(i);

Bundle extras = getIntent().getExtras();
if (extras != null) {
    String result = extras.getString("id1");                
    System.out.println("yeah"+result);
}
Run Code Online (Sandbox Code Playgroud)