0

I'm using a tabs adapter with Fragments and ActionBarSherlock. I want to use a datepicker dialog in one of my fragments, called from an onClick event on one of the buttons. The app is crashing when I click on either of the buttons to call up the DatePickerDialog. I don't have the first clue what's wrong.

I've gotten code from this answer: Show dialog from fragment?

and this answer: getSupportFragmentManager() is undefined

Here is the fragment with the button that is clicked to get the 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);

    }
}

Here is the code for the 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;
  }
 }

Here is my 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)
4

1 回答 1

1

I ditched the whole process of calling up another fragment so now my ReportsFragment onActivityCreated looks like this:

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

        }

    });
}
于 2013-05-16T18:20:38.163 回答