当我尝试使用意图在捆绑中发送多个值时,它仅发送一个字符串(第一个)。单击FAB即可结束活动(尽管这可能无关紧要。
public static final String TITLE_REPLY = "TITLE";
public static final String DESC_REPLY = "DESC";
public static final String DATE_REPLY = "DATE";
public static final String TIME_REPLY = "TIME";
public static final String DATELONG_REPLY = "DATELONG";
@Override
protected void onCreate(Bundle savedInstanceState) {
final Calendar c = Calendar.getInstance();
final int hour = c.get(Calendar.HOUR_OF_DAY);
final int minute = c.get(Calendar.MINUTE);
final int year = c.get(Calendar.YEAR);
final int month = c.get(Calendar.MONTH);
final int day = c.get(Calendar.DAY_OF_MONTH);
DatePickerFragment.year = year;
DatePickerFragment.day = day;
DatePickerFragment.month = month;
TimePickerFragment.min = minute;
TimePickerFragment.hour = hour+1;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_reminder);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ActionBar ab = getSupportActionBar();
ab.setDisplayHomeAsUpEnabled(true);
final ReminderViewModel mReminderViewModel = ViewModelProviders.of(this).get(ReminderViewModel.class);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (TimePickerFragment.hour+TimePickerFragment.min+DatePickerFragment.day+DatePickerFragment.month+DatePickerFragment.year<day+month+year+hour+minute){
Snackbar.make(view,"Time is in the past!",Snackbar.LENGTH_SHORT).show();
}
else{
DateFormat formatter = new SimpleDateFormat("yyyyMdHm");
String dateString = String.valueOf(DatePickerFragment.year)+String.valueOf(DatePickerFragment.month)+String.valueOf(DatePickerFragment.day)+String.valueOf(TimePickerFragment.hour)+String.valueOf(TimePickerFragment.min);
Date date = null;
try {
date = formatter.parse(dateString);
} catch (ParseException e) {
e.printStackTrace();
}
if (Title !=null&&!Title.isEmpty()) {
if(Desc != null&&Desc.isEmpty()){
Desc = "No description";
}
long dateLong = date.getTime() / 1000;
Intent replyIntent = new Intent(getApplicationContext(),MainActivity.class);
replyIntent.putExtra(TITLE_REPLY, Title);
replyIntent.putExtra(DESC_REPLY, Desc);
replyIntent.putExtra(DATE_REPLY, Date);
replyIntent.putExtra(TIME_REPLY, time);
//bundle.putLong(DATELONG_REPLY, dateLong);
replyIntent.putExtras(replyIntent);
setResult(RESULT_OK, replyIntent);
finish();
}
else{
//Code for blank title
}
}
}
});
}
当我尝试从活动中获取字符串时,我仅获取标题的值,其余为空。
这是我的活动结果代码:
public void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode,resultCode,data);
String title = data.getStringExtra(TITLE_REPLY);
String desc = String.valueOf(data.getExtras().get(DESC_REPLY));
String dateTime = String.valueOf(data.getExtras().get(DATELONG_REPLY));
Log.d("Title", title);
Log.d("Desc", desc);
Log.d("DateLong",dateTime);
}
使用此代码,“ Title”的日志输出返回实际的字符串,但其余部分返回null(由于使用对象和String.valueOf,字符串为null) 如何在意图中发送多个字符串?我四处搜寻,这似乎是正确的方法,但底部here上的答案似乎相反。我这样做对吗?这有可能吗?