Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying DatePickerDialog in a Fragment

I am creating a fragment that have a EditText that when I clicked it, there will be a DatePickerDialog showing up.. but my app is crashing whenever I click the EditText..

The Error is in this line:

    return new DatePickerDialog(getActivity(), (DatePickerDialog.OnDateSetListener) getActivity(), year, month, day);

My expense FRAGMENT Java codes (the datepick codes only):

@Nullable
    @Override
    public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_expense, container, false);

            mDisplayDate = (EditText) view.findViewById(R.id.datePick);

            mDisplayDate.setOnClickListener(new View.OnClickListener(){
                @Override
            public void onClick(View v) {
                DialogFragment datePicker = new DatePickerFragment();
                datePicker.show(getFragmentManager(), "date picker");
            }
        });
        return view;
    }

    @Override
    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, month);
        c.set(Calendar.DAY_OF_MONTH, dayOfMonth);
        String currentDate = DateFormat.getDateInstance().format(c.getTime());

        dates = Integer.toString(dayOfMonth);
        months = Integer.toString(month);
        years = Integer.toString(year);

        mDisplayDate.setText(currentDate);

    }

DatePickerFragment java

package com.example.admin.test2;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;

import java.util.Calendar;

public class DatePickerFragment extends DialogFragment {

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);

        return new DatePickerDialog(getActivity(), (DatePickerDialog.OnDateSetListener) getActivity(), year, month, day);
    }
}

THE ERROR

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.admin.test2, PID: 25901
    java.lang.ClassCastException: com.example.admin.test2.ScreenOne cannot be cast to android.app.DatePickerDialog$OnDateSetListener
        at com.example.admin.test2.DatePickerFragment.onCreateDialog(DatePickerFragment.java:22)
        at android.support.v4.app.DialogFragment.onGetLayoutInflater(DialogFragment.java:330)
        at android.support.v4.app.Fragment.performGetLayoutInflater(Fragment.java:1308)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1460)
        at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
        at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:802)
        at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625)
        at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411)
        at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273)
        at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:733)
        at android.os.Handler.handleCallback(Handler.java:789)
        at android.os.Handler.dispatchMessage(Handler.java:98)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6944)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
like image 608
Richard Johnson Avatar asked Dec 14 '22 12:12

Richard Johnson


2 Answers

ExpenseFragment implements DatePickerDialog.OnDateSetListener, so you need to use getTargetFragment() instead of getActivity().

return new DatePickerDialog(getActivity(), (DatePickerDialog.OnDateSetListener) getTargetFragment(), year, month, day);

Then use setTargetFragment when creating DatePickerFragment.

mDisplayDate.setOnClickListener(new View.OnClickListener(){
    @Override
    public void onClick(View v) {
        DialogFragment datePicker = new DatePickerFragment();
        datePicker.setTargetFragment(ExpenseFragment.this, 0);
        datePicker.show(getFragmentManager(), "date picker");
    }
});
like image 134
Son Truong Avatar answered Dec 28 '22 11:12

Son Truong


The listener is implemented in your parent Fragment, not your parent Activity, so you have to replace getActivity() with getParentFragment()

return new DatePickerDialog(getActivity(), (DatePickerDialog.OnDateSetListener) getParentFragment(), year, month, day);
like image 41
Ahmed Hegazy Avatar answered Dec 28 '22 11:12

Ahmed Hegazy