bundle在设置为intent活动后为null



我正在设置在活动之间遍历的意图。但是这里的bundle在设置intent之后为null。这是我的代码,

NewCourseActivity.java
AdapterView.OnItemSelectedListener mItemSelectedListener = new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
CourseSelected=parent.getSelectedItem().toString();
switch (view.getId()) {
case R.id.BESPINNER:
String becourse=parent.getSelectedItem().toString();
Intent intent = new Intent(NewCourseActivity.this,BEViewCourseActivity.class);
Bundle data1 = new Bundle();
data1.putString("course",becourse);
intent.putExtras(data1);
startActivity(intent);
break; } }
BEViewCourseActivity.java:
CourseSelected=findViewById(R.id.courseSelected);

Intent intent = getIntent();
Bundle data = intent.getExtras();
if(data != null){
String selectedcourse = data.getString("course");
CourseSelected.setText(selectedcourse);
}
else{
CourseSelected.setText("Failed");
}

将数据从一个活动传递到另一个活动:

// updated becourse String
String becourse = parent.getItemAtPosition(position).toString();
Intent intent = new Intent(NewCourseActivity.this , BEViewCourseActivity.class);
intent.putExtra ( "course" , becourse  );
startActivity ( intent );

然后接收传递的数据:

Bundle extras = getIntent().getExtras();
if (extras != null) {
String stringValue = extras.getString("course"); // string must be the same as the passed one
}

最新更新