在将数据从片段传递到Activity时获得Null值

Ram*_*amz 3 android bundle android-intent android-fragmentactivity

我正在使用片段并遇到以下问题:

  1. 从片段传递数据时,我使用bundle传递整数值并通过intent发送它们.
  2. 我是通过使用意图来调用它们的.
  3. 但是当我打印时,我只得到Null值.

来自fragment:

Bundle bundle = new Bundle();
bundle.putInt("myData", x);
Intent in=new Intent(getActivity(),B.class);
in.putExtra("xy", bundle);
startActivity(in);
Run Code Online (Sandbox Code Playgroud)

在Activity:

Intent in=getIntent();
Bundle bundle = getIntent().getExtras();
int value = bundle.getInt("myData");
Log.v("in mainactivity",""+value);
Run Code Online (Sandbox Code Playgroud)

这里得到Null值.我希望你能理解这个问题.

Bla*_*elt 9

你必须使用(注意s末尾的putExtras)

 in.putExtras(bundle);
Run Code Online (Sandbox Code Playgroud)

否则你无法直接检索它 getIntent().getExtras();