我正在使用带有Fragments和ActionBarSherlock的标签适配器。我想在我的一个片段中使用一个datepicker对话框,从其中一个按钮上的onClick事件调用。当我点击任一按钮调用DatePickerDialog时,应用程序崩溃。我没有第一个线索有什么不对。
我从这个答案得到了代码:Show dialog from fragment?
和这个回答:getSupportFragmentManager() is undefined
这是带有按钮的片段,单击该按钮以获取DatePickerDialog:
public class ReportsFragment extends SherlockDialogFragment {
//Buttons to call up DatePickerDialog
Button btnStartDate;
Button btnEndDate;
static String TAG = "ReportsFragment";
private DataBaseHelper myDbHelper;
//beginning and start dates to display
protected Calendar StartDate;
protected Calendar EndDate;
//factory method
static ReportsFragment newInstance() {
// Create a new fragment instance
ReportsFragment reportFrag = new ReportsFragment();
return reportFrag;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d(TAG, "onCreateView");
View distance =inflater.inflate(R.layout.reports_frag, container, false);
btnStartDate = (Button) distance.findViewById(R.id.btnStartDate);
btnEndDate = (Button)distance.findViewById(R.id.btnEndDate);
btnSendEmail = (Button)distance.findViewById(R.id.btnSendEmail);
myDbHelper = new DataBaseHelper(getActivity());
CalcDate();
return distance;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
Log.d(TAG, "onActivityCreated");
super.onActivityCreated(savedInstanceState);
//Call up datepicker dialog for StartDate
btnStartDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DatePickerDialogFragment df = DatePickerDialogFragment.newInstance(true);
FragmentTransaction ft = getSherlockActivity().getSupportFragmentManager().beginTransaction();
df.show(ft, "dialog");
}
});
//Call up datepicker dialog for EndDate
btnEndDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DatePickerDialogFragment df = DatePickerDialogFragment.newInstance(false);
FragmentTransaction ft = getSherlockActivity().getSupportFragmentManager().beginTransaction();
df.show(ft, "dialog");
}
});
}
public Dialog onCreateDialog(Bundle SavedInstanceState) {
return null;
}
//Figures out which dates to display
protected void CalcDate() {
StartDate = new GregorianCalendar();
EndDate = new GregorianCalendar();
Log.d(TAG, "We're here");
//figure out which year to show.
//starting date will always be less than "now"
mGPSDC = myDbHelper.GetClosestEntryToNow();
if ((mGPSDC.getStartLatitude() != 0) && (mGPSDC.getEndLatitude() != 0)) {
Log.d("Start Lat ", "value =" + mGPSDC.getStartLatitude());
Log.d("Start Long ", "value =" + mGPSDC.getStartLongitude());
StartDate.setTimeInMillis(mGPSDC.getStartUnixEpoch() * 1000);
Log.d("Start Year", "value =" + StartDate.get(Calendar.YEAR));
}
int Year = StartDate.get(Calendar.YEAR);
//set to January 1 of the closest entry date, or this year if there is no entries
StartDate.set(Year, 0, 1);
StartDate.set(Calendar.MINUTE, 0);
StartDate.set(Calendar.HOUR, 0);
//set to December 31 of the closest entry date year or this year if there is no entries
EndDate.set(Year+1, 0, 1);
EndDate.set(Calendar.HOUR, 0);
EndDate.set(Calendar.MINUTE, 0);
}
}
以下是DatePickerDialogFragment的代码:
public class DatePickerDialogFragment extends ReportsFragment implements DatePickerDialog.OnDateSetListener {
static boolean mStartOrEnd = true;
private int mLMPYear;
private int mLMPMonth;
private int mLMPDay;
public static DatePickerDialogFragment newInstance() {
DatePickerDialogFragment d = new DatePickerDialogFragment();
return d;
}
/*
* Param: StartOrEnd true if StartDate, false if EndDate
*/
public static DatePickerDialogFragment newInstance(boolean StartOrEnd) {
mStartOrEnd = StartOrEnd;
DatePickerDialogFragment d = new DatePickerDialogFragment();
return d;
}
public Dialog onCreateDialog(Bundle savedInstanceState) {
CalcDate();
Calendar theDate = new GregorianCalendar();
theDate = mStartOrEnd ? StartDate : EndDate;
mLMPYear = theDate.get(Calendar.YEAR);
mLMPMonth = theDate.get(Calendar.MONTH);
mLMPDay = theDate.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(getActivity(), this, mLMPYear, mLMPMonth, mLMPDay);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
mLMPYear = year;
mLMPMonth = month;
mLMPDay = day;
}
}
这是我的logcat:
05-15 17:32:42.546: E/AndroidRuntime(30069): Uncaught handler: thread main exiting due to uncaught exception
05-15 17:32:42.546: E/AndroidRuntime(30069): android.util.AndroidRuntimeException: requestFeature() must be called before adding content
05-15 17:32:42.546: E/AndroidRuntime(30069): at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:172)
05-15 17:32:42.546: E/AndroidRuntime(30069): at com.android.internal.app.AlertController.installContent(AlertController.java:198)
05-15 17:32:42.546: E/AndroidRuntime(30069): at android.app.AlertDialog.onCreate(AlertDialog.java:251)
05-15 17:32:42.546: E/AndroidRuntime(30069): at android.app.Dialog.dispatchOnCreate(Dialog.java:308)
05-15 17:32:42.546: E/AndroidRuntime(30069): at android.app.Dialog.show(Dialog.java:225)
05-15 17:32:42.546: E/AndroidRuntime(30069): at android.app.DatePickerDialog.show(DatePickerDialog.java:129)
05-15 17:32:42.546: E/AndroidRuntime(30069): at android.support.v4.app.DialogFragment.onStart(DialogFragment.java:385)
05-15 17:32:42.546: E/AndroidRuntime(30069): at com.webnation.gpsdistancetrackingemail.ReportsFragment.onStart(ReportsFragment.java:198)
05-15 17:32:42.546: E/AndroidRuntime(30069): at android.support.v4.app.Fragment.performStart(Fragment.java:1336)
05-15 17:32:42.546: E/AndroidRuntime(30069): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:907)
05-15 17:32:42.546: E/AndroidRuntime(30069): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1083)
05-15 17:32:42.546: E/AndroidRuntime(30069): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:635)
05-15 17:32:42.546: E/AndroidRuntime(30069): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1431)
05-15 17:32:42.546: E/AndroidRuntime(30069): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:420)
05-15 17:32:42.546: E/AndroidRuntime(30069): at android.os.Handler.handleCallback(Handler.java:587)
05-15 17:32:42.546: E/AndroidRuntime(30069): at android.os.Handler.dispatchMessage(Handler.java:92)
05-15 17:32:42.546: E/AndroidRuntime(30069): at android.os.Looper.loop(Looper.java:123)
05-15 17:32:42.546: E/AndroidRuntime(30069): at android.app.ActivityThread.main(ActivityThread.java:4363)
05-15 17:32:42.546: E/AndroidRuntime(30069): at java.lang.reflect.Method.invokeNative(Native Method)
05-15 17:32:42.546: E/AndroidRuntime(30069): at java.lang.reflect.Method.invoke(Method.java:521)
05-15 17:32:42.546: E/AndroidRuntime(30069): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
05-15 17:32:42.546: E/AndroidRuntime(30069): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
05-15 17:32:42.546: E/AndroidRuntime(30069): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
我放弃了调用另一个片段的整个过程,所以现在我的ReportsFragment onActivityCreated看起来像这样:
@Override
public void onActivityCreated(Bundle savedInstanceState) {
Log.d(TAG, "onActivityCreated");
super.onActivityCreated(savedInstanceState);
SetTextDates();
btnStartDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
int mYear = year;
int mMonth = monthOfYear;
int mDay = dayOfMonth;
//updateDisplay();
}
};
DatePickerDialog d = new DatePickerDialog(getActivity(),
DialogFragment.STYLE_NO_TITLE, mDateSetListener, StartDate.get(Calendar.YEAR), StartDate.get(Calendar.MONTH), StartDate.get(Calendar.DAY_OF_MONTH));
d.show();
}
});
btnEndDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
int mYear = year;
int mMonth = monthOfYear;
int mDay = dayOfMonth;
//updateDisplay();
}
};
DatePickerDialog d = new DatePickerDialog(getActivity(),
DialogFragment.STYLE_NO_TITLE, mDateSetListener, EndDate.get(Calendar.YEAR), EndDate.get(Calendar.MONTH), EndDate.get(Calendar.DAY_OF_MONTH));
d.show();
}
});
}