有重定向到页面的问题

时间:2017-06-14 09:26:02

标签: java android android-activity

我正在使用Android Studio,在我的应用程序中有2页。一个是搜索页面,用户必须键入信息才能进行搜索。其中一个EditTexts需要日期和时间,而不是输入,单击EditText会将用户重定向到第二个页面,他将从CalendarView和Time Picker中选择日期和时间。当他点击“完成”按钮时,日期和时间将被填充到前面提到的EditText中。

这个问题?当发生这种情况时应用程序崩溃,请帮我处理代码。

以下是第一页的代码:

public class SearchActivity extends AppCompatActivity {
EditText etPickUp, etDropOff, etRentS, etRentE;
Button btnBack, btnSearch;

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

    etPickUp = (EditText) findViewById(R.id.etPickUp);
    etDropOff = (EditText) findViewById(R.id.etDropOff);

    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);
            startActivity(rentS);
        }
    });

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

    String date1="";
    String time1="";
    Bundle bundle1 = getIntent().getExtras();
    if(bundle1 != null)
    {
        date1 = bundle1.getString("Date1");
        time1 = bundle1.getString("Time1");
    }
    etRentS.setText(date1);
    etRentS.setText(time1);


    btnSearch = (Button) findViewById(R.id.btnSearch);
    btnSearch.setOnClickListener(new OnClickListener(){
        public void onClick(View v){

        }
    });

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
}

这是第二页的代码:

public class RentStartActivity extends AppCompatActivity {

Button btnDone1, btnCancel1;
CalendarView calendar1;
TimePicker time1;

@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());
            startActivity(done1);
        }
    });

    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);
        }
    });

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
}

谢谢!

2 个答案:

答案 0 :(得分:0)

您忘记了在定义intent对象时应该启动哪个活动。

            Intent done1 = new Intent();
            done1.putExtra("Date1", calendar1.getDate());
            done1.putExtra("Time1", time1.getDrawingTime());
            startActivity(done1);

你应该添加这样的东西

Intent done1=new Intent(SearchActivity.this, RentStartActivity.class);

答案 1 :(得分:0)

您想要使用活动结果。您需要先使用以下代码打开第二个活动:

startActivityForResult( RentStartActivity, RentStartActivityResultCode )

其中RentStartActivityResultCode是用于标识结果的常量int。

在第二个活动中,在完成按钮的动作处理程序中使用此代码发回结果:

Intent output = new Intent();
output.putExtra("Date1", calendar1.getDate().getTime() );
output.putExtra("Hour", time1.getHour() );
output.putExtra("Minute", time1.getMinute() );
setResult( RESULT_OK, output );
finish();

最后,在第一个活动中使用此代码来读取结果:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if( requestCode == RentStartActivityResultCode && resultCode == RESULT_OK && data != null ) {
        Date date1 = new Date( data.getLongExtra("Date1") );
        int hour = data.getIntExtra("Hour");
        int minute = data.getIntExtra("Minute");
    }
}