在片段中显示 DatePickerDialog

Posted

技术标签:

【中文标题】在片段中显示 DatePickerDialog【英文标题】:Displaying DatePickerDialog in a Fragment 【发布时间】:2019-07-28 09:13:34 【问题描述】:

我正在创建一个具有 EditText 的片段,当我单击它时,会显示一个 DatePickerDialog .. 但是每当我单击 EditText 时我的应用程序就会崩溃..

错误在这一行:

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

我的费用片段 Java 代码(仅限 datepick 代码):

@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);
    

错误

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)

【问题讨论】:

【参考方案1】:

ExpenseFragment 实现了DatePickerDialog.OnDateSetListener,所以你需要使用getTargetFragment() 而不是getActivity()

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

然后在创建DatePickerFragment时使用setTargetFragment

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");
    
);

【讨论】:

【参考方案2】:

监听器是在你的父Fragment中实现的,而不是你的父Activity,所以你必须用getParentFragment()替换getActivity()

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

【讨论】:

这是对我有用的解决方案。

以上是关于在片段中显示 DatePickerDialog的主要内容,如果未能解决你的问题,请参考以下文章

为啥我的 ListView 显示在一个片段中而不是另一个片段中?

如何在 Android 中的特定片段容器中显示片段

回收站视图未显示在片段中

如何使用 tabLayout 在 ViewPager 中显示片段?

onbackpressed 数据未显示在片段中

ListView 未显示在片段中