CalendarView返回null

时间:2017-06-19 03:47:50

标签: android android-activity timepicker calendarview

我正在使用Android Studio并拥有2项活动,SearchActivity和RentStartActivity。在我的SearchActivity中,我有一个EditText,用户将点击它,它会将他重定向到下一个Activity。然后在下一个Activity中,他将从CalendarView和TimePicker中选择一个日期和时间,当点击“完成”按钮时,用户将返回到SearchActivity,日期和时间将被设置为同一EditText中的文本。他点击了。

问题是,它返回null。

我的SearchActivity代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_search);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    etRentS = (EditText) findViewById(R.id.etRentS);
    etRentS.setFocusable(false);
    etRentS.setClickable(true);
    etRentS.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v){
            Intent rentS = new Intent(SearchActivity.this, RentStartActivity.class);
            startActivityForResult(rentS, 1);
        }
    });

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if(requestCode == 1)
    {
        String date1 = data.getStringExtra("Date1");
        String time1 = data.getStringExtra("Time1");
        etRentS.setText(date1 + time1);
    }
}

这是我的RentStartActivity代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_rent_start);


    calendar1 = (CalendarView) findViewById(R.id.calendar1);
    time1 = (TimePicker) findViewById(R.id.time1);

    btnDone1 = (Button) findViewById(R.id.btnDone1);
    btnDone1.setOnClickListener(new OnClickListener(){
        public void onClick(View v){
            Intent done1 = new Intent();
            done1.putExtra("Date1", calendar1.getDate());
            done1.putExtra("Time1", time1.getDrawingTime());
            setResult(1, done1);
            finish();
        }
    });

    btnCancel1 = (Button) findViewById(R.id.btnCancel1);
    btnCancel1.setOnClickListener(new OnClickListener(){
        public void onClick(View v){
            Intent cancel1 = new Intent(RentStartActivity.this, SearchActivity.class);
            startActivity(cancel1);
        }
    });

}

请帮忙谢谢!! :)

2 个答案:

答案 0 :(得分:0)

在RentStartActivity中

你必须做setResult(Activity.RESULT_OK, done1);

和SearchActivity

onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 1) {
        if (resultCode == Activity.RESULT_OK) {
            /* what you were doing */
        }
    }
}

答案 1 :(得分:0)

您应该在if语句中检查结果代码 -

if(requestCode == 1 && resultCode == 1)
    {

..............
}

这可能会对你有帮助。