Moving to another screen not working

274 views Asked by At

I am developing and using Android Studio. I have a screen that uses a calendar to get a date from users input. The trouble as soon as I added the code to this, my button to move to another screen has stopped working. I have spent a few hours on this and cannot find where I am going wrong. I suspect its to do with the order of my code but struggling here.

here is the code from the java class

//class starts here
    package com.MSL.claimssolutions1;

    import android.app.Activity;
    import android.app.DatePickerDialog;
    import android.app.Dialog;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.DatePicker;
    import android.widget.EditText;
    import android.widget.TextView;

    import java.util.Calendar;


    public class accident_details_activity extends Activity
    {
        //set variables
        private int mYear;
        private int mMonth;
        private int mDay;

        private TextView mDateDisplay;
        private Button mPickDate;

        static final int DATE_DIALOG_ID = 0;
        accident_details_activity ob;
        public void onCreate(Bundle icicle)
        {
            //listen for click from button to move to next screen

// this is where I think my code is going wrong - I think should be moved elsewhere, not sure though
            super.onCreate(icicle);
            setContentView(R.layout.accident_details_activity);
            Button StepFive = (Button) findViewById(R.id.StepFive_button);
            StepFive.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
            //move to next screen - i.e. upload files
                Intent i = new Intent(accident_details_activity.this, upload_file_activity.class);
                startActivity(i);
            }

            });

        //add calendar functionality

            //@Override
            super.onCreate(icicle);
            setContentView(R.layout.accident_details_activity);

            mDateDisplay = (EditText) findViewById(R.id.AccidentDate);
            mPickDate = (Button) findViewById(R.id.myDatePickerButton);

            mPickDate.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    showDialog(DATE_DIALOG_ID);
                }
            });

            // get the current date
            final Calendar c = Calendar.getInstance();
            mYear = c.get(Calendar.YEAR);
            mMonth = c.get(Calendar.MONTH);
            mDay = c.get(Calendar.DAY_OF_MONTH);

            // display the current date
            updateDisplay();
        }
        public void setOb( accident_details_activity obA){
            this.ob=obA;
        }
    //update text field with date selected from calendar
        private void updateDisplay() {
            this.mDateDisplay.setText(
                    new StringBuilder()
                            // Month is 0 based so add 1
                            .append(mDay).append("-")
                            .append(mMonth + 1).append("-")
                            .append(mYear).append(" "));
        }
        private DatePickerDialog.OnDateSetListener mDateSetListener =
                new DatePickerDialog.OnDateSetListener() {
                    public void onDateSet(DatePicker view, int year,
                                          int monthOfYear, int dayOfMonth) {
                        mYear = year;
                        mMonth = monthOfYear;
                        mDay = dayOfMonth;
                        updateDisplay();
                    }
                };
        @Override
        protected Dialog onCreateDialog(int id) {
            switch (id) {
                case DATE_DIALOG_ID:
                    return new DatePickerDialog(this,
                            mDateSetListener,
                            mYear, mMonth, mDay);
            }
            return null;
        }

    }
1

There are 1 answers

0
Greg Giacovelli On

You are calling setContentView() then looking up the view to click, setting a listener AND THEN you are calling setContentView() again. This will wipe out the view you just setup. Not mention that reference to the StepFive button will no longer be in the same hierarchy.

Only call setContentView once and then bind your variables and listeners after.