1

我有一个日期选择器设置,它不断使应用程序崩溃。我遵循了本教程,但我不知道为什么它不起作用。

我将发布下面类的完整代码以显示应用程序崩溃时我得到的 logcat

主要代码:

public class WorkoutChoice extends Activity
{
 private TextView mDateDisplay;
 private Button mPickDate;
 private int mYear,mMonth,mDay;
 private int cYear,cMonth,cDay;
 static final int DATE_DIALOG_ID = 0;
 Button   mButton;
 EditText cweight;
 EditText nweight;
 TextView t;
 String s,s2,cDate;
 int current,target;
 DataBaseHelper db = new DataBaseHelper(this);
@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.choice);

    mDateDisplay = (TextView) findViewById(R.id.dateDisplay);
    mPickDate = (Button) findViewById(R.id.pickDate);
    mPickDate.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v) {
            showDialog(DATE_DIALOG_ID);
        }
    });
    final Calendar c = Calendar.getInstance();
    cYear = c.get(Calendar.YEAR);
    cMonth = c.get(Calendar.MONTH);
    cDay = c.get(Calendar.DAY_OF_MONTH);
    cDate = (""+cYear+""+cMonth+""+cDay);
    Date past = new Date(112, cMonth, cDay); // current Date
    Date today = new Date(112, 11, 18); // date Choosen by the user
    final int days = Days.daysBetween(new DateTime(past), new DateTime(today)).getDays()+1;
     mDateDisplay.setText(cDate);
     //display the current date (this method is below)
     updateDisplay();

    mButton = (Button)findViewById(R.id.get);
    cweight   = (EditText)findViewById(R.id.cweight);
    nweight   = (EditText)findViewById(R.id.nweight);
    t = (TextView)findViewById(R.id.out);

    mButton.setOnClickListener(
        new View.OnClickListener()
        {
            public void onClick(View view)
            {
                db.open();
               s = WorkoutChoice.this.cweight.getText().toString();
               current =  Integer.parseInt(s);
               s2 = WorkoutChoice.this.nweight.getText().toString();
               target =  Integer.parseInt(s2);
               db.deleteFirst();
               db.insertTitle("001", current, target);
               db.close();
            }

        });

}
public void convert(View view) 
{
    Intent i = new Intent();
    i.setClassName("com.b00348312.workout","com.b00348312.workout.convert");
    startActivity(i); 
}
 private void updateDisplay() {
        mDateDisplay.setText(
            new StringBuilder()
                    // Month is 0 based so add 1
                    .append(mMonth + 1).append("-")
                    .append(mDay).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;
            }
}

日志猫:

05-26 21:37:25.928: E/AndroidRuntime(334): FATAL EXCEPTION: main
05-26 21:37:25.928: E/AndroidRuntime(334): java.lang.IllegalArgumentException: current should be >= start and <= end
05-26 21:37:25.928: E/AndroidRuntime(334):  at android.widget.NumberPicker.setCurrent(NumberPicker.java:288)
05-26 21:37:25.928: E/AndroidRuntime(334):  at android.widget.DatePicker.updateDaySpinner(DatePicker.java:364)
05-26 21:37:25.928: E/AndroidRuntime(334):  at android.widget.DatePicker.updateSpinners(DatePicker.java:350)
05-26 21:37:25.928: E/AndroidRuntime(334):  at android.widget.DatePicker.init(DatePicker.java:346)
05-26 21:37:25.928: E/AndroidRuntime(334):  at android.app.DatePickerDialog.<init>(DatePickerDialog.java:124)
05-26 21:37:25.928: E/AndroidRuntime(334):  at android.app.DatePickerDialog.<init>(DatePickerDialog.java:83)
05-26 21:37:25.928: E/AndroidRuntime(334):  at com.b00348312.workout.WorkoutChoice.onCreateDialog(WorkoutChoice.java:123)
05-26 21:37:25.928: E/AndroidRuntime(334):  at android.app.Activity.onCreateDialog(Activity.java:2472)
05-26 21:37:25.928: E/AndroidRuntime(334):  at android.app.Activity.createDialog(Activity.java:881)
05-26 21:37:25.928: E/AndroidRuntime(334):  at android.app.Activity.showDialog(Activity.java:2547)
05-26 21:37:25.928: E/AndroidRuntime(334):  at android.app.Activity.showDialog(Activity.java:2514)
05-26 21:37:25.928: E/AndroidRuntime(334):  at com.b00348312.workout.WorkoutChoice$2.onClick(WorkoutChoice.java:56)
05-26 21:37:25.928: E/AndroidRuntime(334):  at android.view.View.performClick(View.java:2408)
05-26 21:37:25.928: E/AndroidRuntime(334):  at android.view.View$PerformClick.run(View.java:8816)
05-26 21:37:25.928: E/AndroidRuntime(334):  at android.os.Handler.handleCallback(Handler.java:587)
05-26 21:37:25.928: E/AndroidRuntime(334):  at android.os.Handler.dispatchMessage(Handler.java:92)
05-26 21:37:25.928: E/AndroidRuntime(334):  at android.os.Looper.loop(Looper.java:123)
05-26 21:37:25.928: E/AndroidRuntime(334):  at android.app.ActivityThread.main(ActivityThread.java:4627)
05-26 21:37:25.928: E/AndroidRuntime(334):  at java.lang.reflect.Method.invokeNative(Native Method)
05-26 21:37:25.928: E/AndroidRuntime(334):  at java.lang.reflect.Method.invoke(Method.java:521)
05-26 21:37:25.928: E/AndroidRuntime(334):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)  
05-26 21:37:25.928: E/AndroidRuntime(334):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-26 21:37:25.928: E/AndroidRuntime(334):  at dalvik.system.NativeStart.main(Native Method)
4

2 回答 2

1

您从不初始化mYear, mMonth, 或mDay在使用它们之前。

于 2012-05-26T21:53:42.563 回答
0

这很简单,使用以下,

dtpStart.init(now.getYear(), now.getMonth(), now.getDay(),new OnDateChangedListener() { @Override public void onDateChanged(DatePicker view, int year,int monthOfYear, int dayOfMonth) { dateStart = (Date) new Date(year, monthOfYear,dayOfMonth); } }
其中 dtpStart 是我的日期选择器的 ID,而 dateStart 是一个 java.util.Date 对象,它是全局声明的,以便可以在我的 Activity 中的任何函数/处理程序中访问它

希望能帮助到你!

于 2013-10-01T11:57:12.737 回答