我正在创建一个包含EditText的片段,当我单击它时,将显示一个DatePickerDialog。.但是,只要单击EditText,我的应用程序就会崩溃。
此行中的错误:
return new DatePickerDialog(getActivity(), (DatePickerDialog.OnDateSetListener) getActivity(), year, month, day);
我的支出FRAGMENT Java代码(仅日期选择代码):
@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)
答案 0 :(得分:0)
监听器是在您的父Fragment中实现的,而不是在父Activity中实现的,因此您必须将# Python 2.7
from collections import Set
# in Python 3.x this would be from collections.abc import Set
d = {"one": 1}
print isinstance(d.viewkeys(), Set)
# True
print isinstance(d.viewvalues(), Set)
# False
print d.viewkeys() == d.viewkeys()
# True
print d.viewvalues() == d.viewvalues()
# False
替换为getActivity()
getParentFragment()
答案 1 :(得分:0)
ExpenseFragment
实现了DatePickerDialog.OnDateSetListener
,因此您需要使用getTargetFragment()
而不是getActivity()
。
return new DatePickerDialog(getActivity(), (DatePickerDialog.OnDateSetListener) getTargetFragment(), year, month, day);
然后在创建setTargetFragment
时使用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");
}
});