Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

java.lang.IllegalStateException when i click on button to open SlideDateTimePicker #57

Open
patelhari opened this issue Jun 1, 2018 · 8 comments

Comments

@patelhari
Copy link

patelhari commented Jun 1, 2018

public class DateAndTimePickerFragment extends Fragment {
private SimpleDateFormat mFormatter = new SimpleDateFormat("MMMM dd yyyy hh:mm aa");
private Button mButton;
    private SlideDateTimeListener listener = new SlideDateTimeListener() {

        @Override
        public void onDateTimeSet(Date date)
        {
            Toast.makeText(mcontext,
                    mFormatter.format(date), Toast.LENGTH_SHORT).show();
        }

        // Optional cancel listener
        @Override
        public void onDateTimeCancel()
        {
            Toast.makeText(mcontext,
                    "Canceled", Toast.LENGTH_SHORT).show();
        }
    };
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mButton = v.findViewById(R.id.button);
mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new SlideDateTimePicker.Builder(getActivity().getSupportFragmentManager())
                        .setListener(listener)
                        .setInitialDate(new Date())
                        .build()
                        .show();
            }
        });
}
public class CustomViewPager extends ViewPager
{
@Override
public void onMeasure(...)
{
 int height = 0;

        for (int i = 0; i < getChildCount(); i++)
        {
            View child = getChildAt(i);
            child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
            int h = child.getMeasuredHeight();
            if (h > height)
                height = h;
        }

        heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);   //error on this line

        mDatePicker = (DatePicker) findViewById(R.id.datePicker);
        mTimePicker = (TimePicker) findViewById(R.id.timePicker);
}
}
06-01 16:28:42.382 15726-15726/***** E/UncaughtException: java.lang.IllegalStateException: Fragment DateFragment{6011f27 #0 id=0x7f0903e0 android:switcher:2131297248:0} declared target fragment SlideDateTimeDialogFragment{52255d4 #5 tagSlideDateTimeDialogFragment} that does not belong to this FragmentManager!
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1352)
        at android.support.v4.app.FragmentTransition.addToFirstInLastOut(FragmentTransition.java:1109)
        at android.support.v4.app.FragmentTransition.calculateFragments(FragmentTransition.java:996)
        at android.support.v4.app.FragmentTransition.startTransitions(FragmentTransition.java:99)
        at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2364)
        at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2322)
        at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:2199)
        at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:651)
        at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:145)
        at android.support.v4.view.ViewPager.populate(ViewPager.java:1236)
        at android.support.v4.view.ViewPager.populate(ViewPager.java:1084)
        at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1614)
        at alastrine.slidedatetimepicker.CustomViewPager.onMeasure(CustomViewPager.java:70)
        at android.view.View.measure(View.java:18804)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5954)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
        at android.view.View.measure(View.java:18804)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5954)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
        at android.view.View.measure(View.java:18804)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5954)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
        at android.view.View.measure(View.java:18804)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5954)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
        at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2643)
        at android.view.View.measure(View.java:18804)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2112)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1203)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1464)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1119)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6060)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
        at android.view.Choreographer.doCallbacks(Choreographer.java:670)
        at android.view.Choreographer.doFrame(Choreographer.java:606)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
        at android.os.Handler.handleCallback(Handler.java:746)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5443)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
06-01 16:28:42.383 15726-15906/**** E/FirebaseCrash: Firebase Crash Reporting not enabled
06-01 16:28:42.384 15726-15726/****l E/AndroidRuntime: FATAL EXCEPTION: main
    Process: ****, PID: 15726
    java.lang.IllegalStateException: Fragment DateFragment{6011f27 #0 id=0x7f0903e0 android:switcher:2131297248:0} declared target fragment SlideDateTimeDialogFragment{52255d4 #5 tagSlideDateTimeDialogFragment} that does not belong to this FragmentManager!
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1352)
        at android.support.v4.app.FragmentTransition.addToFirstInLastOut(FragmentTransition.java:1109)
        at android.support.v4.app.FragmentTransition.calculateFragments(FragmentTransition.java:996)
        at android.support.v4.app.FragmentTransition.startTransitions(FragmentTransition.java:99)
        at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2364)
        at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2322)
        at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:2199)
        at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:651)
        at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:145)
        at android.support.v4.view.ViewPager.populate(ViewPager.java:1236)
        at android.support.v4.view.ViewPager.populate(ViewPager.java:1084)
        at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1614)
        at alastrine.slidedatetimepicker.CustomViewPager.onMeasure(CustomViewPager.java:70)
        at android.view.View.measure(View.java:18804)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5954)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
        at android.view.View.measure(View.java:18804)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5954)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
        at android.view.View.measure(View.java:18804)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5954)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
        at android.view.View.measure(View.java:18804)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5954)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
        at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2643)
        at android.view.View.measure(View.java:18804)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2112)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1203)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1464)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1119)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6060)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
        at android.view.Choreographer.doCallbacks(Choreographer.java:670)
        at android.view.Choreographer.doFrame(Choreographer.java:606)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
        at android.os.Handler.handleCallback(Handler.java:746)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5443)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
@nambv
Copy link

nambv commented Jun 19, 2018

@patelhari yes, I face this issue too

@ghost
Copy link

ghost commented Jul 17, 2018

Same error here

@kamleshkarwande
Copy link

kamleshkarwande commented Aug 13, 2018

I am also facing the same error. Happenend after updateing the project to latest version. API 26

@ratriretno
Copy link

any solution ?

@mohitissar
Copy link

same issue here

@patelhari
Copy link
Author

patelhari commented Sep 11, 2018

@nambv , @RonEskinder , @kamleshkarwande , @ratriretno , @mohitissar
For me only the solution is below

change getTargetFragment() to getParentFragment() in DateFragment.java and TimeFragment.java

@Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        try
        {
            mCallback = (DateChangedListener) getParentFragment();
        }
        catch (ClassCastException e)
        {
            throw new ClassCastException("Calling fragment must implement " +
                "DateFragment.DateChangedListener interface");
        }
    }

change in onMeasure fix height value in CustomViewPager.java

@Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        mDatePicker = (DatePicker) findViewById(R.id.datePicker);
        mTimePicker = (TimePicker) findViewById(R.id.timePicker);

        int height = 600;

        for (int i = 0; i < getChildCount(); i++)
        {
            View child = getChildAt(i);
            child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.AT_MOST));
            int h = child.getMeasuredHeight();
            if (h > height)
                height = h;
        }

        heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.AT_MOST);

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //setMeasuredDimension(318, 318);
    }

@fearsom03
Copy link

@nambv , @RonEskinder , @kamleshkarwande , @ratriretno , @mohitissar
@patelhari

Try this library , this library just fixed this issue thats all
implementation 'com.github.longegit:android-SlideDateTimePicker:1.1.1'

@Sijilal
Copy link

Sijilal commented Feb 6, 2020

Thanks, Its really help me alot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants