单击EditText打开片段中的日历

时间:2014-03-06 04:53:55

标签: android android-layout android-fragments android-calendar

我试图在Fragment中单击编辑文本时打开日历。但是当我点击编辑文本时,它会向我抛出空指针异常。 相同的代码在Activity类中工作正常,但在Fargement中工作不正常。

异常

03-05 19:02:39.888: E/AndroidRuntime(1092): FATAL EXCEPTION: main
03-05 19:02:39.888: E/AndroidRuntime(1092): java.lang.NullPointerException
03-05 19:02:39.888: E/AndroidRuntime(1092):     at com.test.newapp.AppTrip.showDialog(AppTrip.java:75)
03-05 19:02:39.888: E/AndroidRuntime(1092):     at com.test.newapp.AppTrip$2.onClick(AppTrip.java:54)
03-05 19:02:39.888: E/AndroidRuntime(1092):     at android.view.View.performClick(View.java:4212)
03-05 19:02:39.888: E/AndroidRuntime(1092):     at android.view.View$PerformClick.run(View.java:17476)
03-05 19:02:39.888: E/AndroidRuntime(1092):     at android.os.Handler.handleCallback(Handler.java:800)
03-05 19:02:39.888: E/AndroidRuntime(1092):     at android.os.Handler.dispatchMessage(Handler.java:100)
03-05 19:02:39.888: E/AndroidRuntime(1092):     at android.os.Looper.loop(Looper.java:194)
03-05 19:02:39.888: E/AndroidRuntime(1092):     at android.app.ActivityThread.main(ActivityThread.java:5371)
03-05 19:02:39.888: E/AndroidRuntime(1092):     at java.lang.reflect.Method.invokeNative(Native Method)
03-05 19:02:39.888: E/AndroidRuntime(1092):     at java.lang.reflect.Method.invoke(Method.java:525)
03-05 19:02:39.888: E/AndroidRuntime(1092):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
03-05 19:02:39.888: E/AndroidRuntime(1092):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
03-05 19:02:39.888: E/AndroidRuntime(1092):     at dalvik.system.NativeStart.main(Native Method)

片段代码

public class AppTrip extends Fragment {
    View rootView;
    EditText from_date, to_date;
    DatePickerDialog.OnDateSetListener date;
    static final int DATE_DIALOG_ID = 999;
    Calendar myCalendar;
    int val;
    Context context;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        context = getActivity();
        rootView = inflater.inflate(R.layout.main, container, false);
        from_date = (EditText) rootView.findViewById(R.id.from_date);
        to_date = (EditText) rootView.findViewById(R.id.to_date);
        from_date.setFocusableInTouchMode(false);
        to_date.setFocusableInTouchMode(false);
        date = new DatePickerDialog.OnDateSetListener() {

            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear,
                    int dayOfMonth) {
                // TODO Auto-generated method stub
                myCalendar.set(Calendar.YEAR, year);
                myCalendar.set(Calendar.MONTH, monthOfYear);
                myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                updateLabel(val);
            }

        };
        from_date.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                showDialog(DATE_DIALOG_ID);
                val = 1;
            }
        });

        to_date.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog(DATE_DIALOG_ID);
                val = 2;
            }
        });
        return rootView;
    }

    protected Dialog showDialog(int dateDialogId) {
        final Calendar now = Calendar.getInstance();
        switch (dateDialogId) {

        case DATE_DIALOG_ID:
            DatePickerDialog _date = new DatePickerDialog(context, date,
                    myCalendar.get(Calendar.YEAR),
                    myCalendar.get(Calendar.MONTH),
                    myCalendar.get(Calendar.DAY_OF_MONTH)) {
                @Override
                public void onDateChanged(DatePicker view, int year,
                        int monthOfYear, int dayOfMonth) {
                    if (year - now.get(Calendar.YEAR) > 1) {
                        view.updateDate(year - 1,
                                myCalendar.get(Calendar.MONTH),
                                myCalendar.get(Calendar.DAY_OF_MONTH));
                    }
                    if (year - now.get(Calendar.YEAR) == 1
                            && monthOfYear > now.get(Calendar.MONTH)) {
                        view.updateDate(year, myCalendar.get(Calendar.MONTH),
                                myCalendar.get(Calendar.DAY_OF_MONTH));
                    }
                    if (year - now.get(Calendar.YEAR) == 1
                            && monthOfYear == now.get(Calendar.MONTH)
                            && dayOfMonth > now.get(Calendar.DAY_OF_MONTH)) {
                        view.updateDate(year, myCalendar.get(Calendar.MONTH),
                                myCalendar.get(Calendar.DAY_OF_MONTH));
                    }
                    if (year < now.get(Calendar.YEAR))

                        view.updateDate(myCalendar.get(Calendar.YEAR),
                                myCalendar.get(Calendar.MONTH),
                                myCalendar.get(Calendar.DAY_OF_MONTH));

                    if (monthOfYear < now.get(Calendar.MONTH)
                            && year == now.get(Calendar.YEAR))
                        view.updateDate(myCalendar.get(Calendar.YEAR),
                                myCalendar.get(Calendar.MONTH),
                                myCalendar.get(Calendar.DAY_OF_MONTH));

                    if (dayOfMonth < now.get(Calendar.DAY_OF_MONTH)
                            && year == now.get(Calendar.YEAR)
                            && monthOfYear == now.get(Calendar.MONTH))
                        view.updateDate(myCalendar.get(Calendar.YEAR),
                                myCalendar.get(Calendar.MONTH),
                                myCalendar.get(Calendar.DAY_OF_MONTH));
                }
            };
            return _date;
        }

        return null;
    }

    private void updateLabel(int val) {
        String myFormat = "dd/MM/yy"; // In which you need put here
        SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
        Log.d("Date value ", "===" + sdf.format(myCalendar.getTime()));
        if (val == 1)
            from_date.setText(sdf.format(myCalendar.getTime()));
        else
            to_date.setText(sdf.format(myCalendar.getTime()));

    }
}

4 个答案:

答案 0 :(得分:1)

改变
 myCalendar.get(Calendar.YEAR),
 myCalendar.get(Calendar.MONTH),
 myCalendar.get(Calendar.DAY_OF_MONTH))

 now.get(Calendar.YEAR),
 now.get(Calendar.MONTH),
 now.get(Calendar.DAY_OF_MONTH))

showDialog() 方法。

您忘记在onCreateView()上初始化 日历myCalendar;

 myCalendar = Calendar.getInstance();

答案 1 :(得分:0)

您正在使用未初始化的myCalendar实例。尝试使用now而不是myCalendar。

答案 2 :(得分:0)

您的代码中的所有内容都可以在您的代码中进行两项更改

首先在onCreateView

中执行此操作
myCalendar = Calendar.getInstance();

并添加此

_date.show();

return _date;

答案 3 :(得分:0)

内部片段showdialog()方法不起作用。你可以在from_date上执行setonclicklistener并在其中调用DatePickerDialog。做与活动类相同的其他事情。 希望能帮到你。