移动到另一个屏幕不工作

时间:2013-12-19 17:01:15

标签: java android android-activity screens

我正在开发和使用Android Studio。我有一个屏幕,使用日历来获取用户输入的日期。一旦我添加了代码,我的按钮移动到另一个屏幕的麻烦已经停止工作。我花了几个小时在这上面,找不到我错的地方。我怀疑它与我的代码的顺序有关但在这里挣扎。

这是java类的代码

//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 个答案:

答案 0 :(得分:2)

您正在调用setContentView(),然后查找视图以单击,设置侦听器,然后再次调用setContentView()。这将消除您刚刚设置的视图。不要提及对StepFive按钮的引用将不再属于同一层次结构。

仅调用setContentView一次,然后在变换后绑定变量和侦听器。