如何解决日期选择器的IllegalArgumentException?

时间:2016-06-05 18:45:05

标签: android datepicker illegalargumentexception datepickerdialog

当我点击edidtext时,我看到了datePicker。

public class DateDialog extends DialogFragment implements DatePickerDialog.OnDateSet`enter code here`Listener {

    EditText txtDate ;
    public DateDialog(View view){
        txtDate=(EditText)view;
    }

    public Dialog onCreateDialog(Bundle savedInstanceState){
        final Calendar c = Calendar.getInstance();
        int day = c.get(Calendar.DAY_OF_MONTH);
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        return new DatePickerDialog(getActivity(), this, year ,month,day) ;
    }

    public void onDateSet (DatePicker view, int year  , int month ,int day ){
        String date = day+ "-" + (month + 1)+"-"+year;
        txtDate.setText(date);
    }
}

然后日期值进入editText

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_page_);    
    }

    public void onStart(){
        super.onStart();
        EditText txtDate = (EditText)findViewById(R.id.editDate);
        txtDate.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(hasFocus){
                    DateDialog dialog = new DateDialog(v);
                    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
                    dialog.show(ft, "DatePicker");
                }
            }
        });

我要添加的方法。

 private String getDate() {
       SimpleDateFormat dateFormat = new SimpleDateFormat
       ("dd-MM-yyyy", Locale.getDefault());
       Date date = new Date();
       return dateFormat.format(date);
   }

    public long addPage(Page page){

        ContentValues values = new ContentValues();
        values.put(PageSQLITE.COL_DATE_PAGE, getDate());
        values.put(PageSQLITE .COL_TITLE_PAGE,page.getTitrePage());

        return bdd.insert(PageSQLITE.TABLE_PAGE, null, values);

当我使用“onclick”保存日期值时,我有IllegalArgumentException。

private String strTitle;
private String datePage;

public void onClickAdd(View v) {
    strTitle = ((EditText) findViewById(R.id.editTitle)).getText().toString();
    datePage = ((EditText) findViewById(R.id.editDate)).getText().toString();

    Page page = new Page(strTitle, new Date( datePage) );
    PageMgr pageMgr = new PageMgr(this);
    pageMgr.open();

    pageMgr.addPage(page);
}

最后我看到了这条消息。

06-04 19:22:15.679 18455-18455/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                   java.lang.IllegalStateException: Could not execute method for android:onClick
                                                       at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
                                                       at android.view.View.performClick(View.java:4452)
                                                       at android.widget.Button.performClick(Button.java:148)
                                                       at android.view.View$PerformClick.run(View.java:18428)
                                                       at android.os.Handler.handleCallback(Handler.java:725)
                                                       at android.os.Handler.dispatchMessage(Handler.java:92)
                                                       at android.os.Looper.loop(Looper.java:176)
                                                       at android.app.ActivityThread.main(ActivityThread.java:5365)
                                                       at java.lang.reflect.Method.invokeNative(Native Method)
                                                       at java.lang.reflect.Method.invoke(Method.java:511)
                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
                                                       at dalvik.system.NativeStart.main(Native Method)
                                                    Caused by: java.lang.reflect.InvocationTargetException
                                                       at java.lang.reflect.Method.invokeNative(Native Method)
                                                       at java.lang.reflect.Method.invoke(Method.java:511)
                                                       at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
                                                       at android.view.View.performClick(View.java:4452) 
                                                       at android.widget.Button.performClick(Button.java:148) 
                                                       at android.view.View$PerformClick.run(View.java:18428) 
                                                       at android.os.Handler.handleCallback(Handler.java:725) 
                                                       at android.os.Handler.dispatchMessage(Handler.java:92) 
                                                       at android.os.Looper.loop(Looper.java:176) 
                                                       at android.app.ActivityThread.main(ActivityThread.java:5365) 
                                                       at java.lang.reflect.Method.invokeNative(Native Method) 
                                                       at java.lang.reflect.Method.invoke(Method.java:511) 
                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102) 
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) 
                                                       at dalvik.system.NativeStart.main(Native Method) 
                                                    Caused by: java.lang.IllegalArgumentException
                                                       at java.util.Date.parse(Date.java:552)
                                                       at java.util.Date.<init>(Date.java:149)

查看我的xml文件android:OnClick

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_top_margin"
    tools:context=".View.AjoutPageActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.25"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            android:id="@+id/textTitre"
            android:text="@string/string_AjoutTitre"
            android:layout_gravity="center"
            android:textSize="22sp"
            />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            android:id="@+id/editTitle"
            android:inputType="text"
            android:layout_gravity="center" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.25"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            android:id="@+id/date"
            android:text="@string/string_AjoutDate"
            android:layout_gravity="center"
            android:textSize="22sp"
          />
        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            android:id="@+id/editDate"
            android:layout_gravity="center"
            android:inputType="date"
            />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.25"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            android:id="@+id/tag"
            android:text="@string/string_AjoutTag"
            android:layout_gravity="center"
            android:textSize="22sp"/>

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            android:id="@+id/editTag"
            android:layout_gravity="center"
            android:inputType="text"
            />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.25"
        android:orientation="horizontal">

        <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/string_buttonAjout"
        android:id="@+id/button"
        android:background="@color/colorBtnAjout"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:onClick="onClickAdd"
        android:gravity="center" />

</LinearLayout>
</LinearLayout>

3 个答案:

答案 0 :(得分:0)

嗯,这里有一个空白字符:"dd-MM-yyyy ";删除它,我认为它会起作用:)

答案 1 :(得分:0)

I finally find the answer just:

    wrong date 
    SimpleDateFormat dateFormat = new SimpleDateFormat
           ("dd-MM-yyyy", Locale.getDefault());

    correct date 
    SimpleDateFormat dateFormat = new SimpleDateFormat
           ("dd/MM/yyyy", Locale.FRENCH);

and similar for

public void onDateSet (DatePicker view, int year , int month ,int day ){
    String date = day+ "/" + (month + 1)+"/"+year ;
    txtDate.setText(date);

答案 2 :(得分:0)

您可以轻松实现此功能。没有必要将EditText对象传递给Date Picker片段。您需要创建一个接口并将此接口实现为activity / fragment。并从Date Picker Fragment调用该方法。

您可以使用Textview而不是Edit Test,并且可以在文本视图上单击显示日期对话框片段。

dateTextView.setonClickListener(new OnClickListener{
  @Override
public void onClick(View v) {
    Dialog dialog;
    switch (v.getId()) {
        case R.id.render_date:
            try {

                Calendar calendar = Calendar.getInstance();
                dialog = new DatePickerDialog(EventDetailActivity.this, this, calendar.get(Calendar.YEAR),
                        calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
                dialog.setCancelable(true);
                dialog.show();
            } catch (ParseException e) {
                e.printStackTrace();
            }
            break;
    }
}

})

在您的activity / fragment中实现DatePickerDialog.OnDateSetListener接口。

@Override
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        TextView textView = (TextView) findViewById(R.id.date);
textView.setText("**Set your date here**");
    }