我在一个页面中使用了3个片段,当我点击其中一个片段时,另外两个片段替换另外两个片段。我想使用像按钮这样的碎片不是一个好习惯,但它对我有用。我使用setCustomAnimations方法为更改设置动画。在第一个动画结束前第二次单击按钮时出现问题。我得到了这个例外:
java.lang.IllegalStateException: No activity
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1113)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1108)
at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1917)
at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1800)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:967)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:454)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5602)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
这是我点击第三个片段时调用的代码:
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
if (currentBottomMenuState == BottomMenuStates.DEFAULT) {
currentBottomMenuState = BottomMenuStates.RADIO;
fragment1a = Fragment1a.newInstance(this, radioService.isPlaying());
ft.setCustomAnimations(R.anim.slide_in_bottom, R.anim.slide_out_top);
ft.replace(bottomMenuContainerResource1, fragment1a);
if (radioService.isPlaying()) {
fragment2a = Fragment2a.newInstance(this);
ft.setCustomAnimations(R.anim.slide_in_top, R.anim.slide_out_bottom);
ft.replace(bottomMenuContainerResource2, fragment2a);
} else {
fragment2b = Fragment2b.newInstance(this);
ft.setCustomAnimations(R.anim.slide_in_top, R.anim.slide_out_bottom);
ft.replace(bottomMenuContainerResource2, fragment2b);
}
} else {
currentBottomMenuState = BottomMenuStates.DEFAULT;
ft.setCustomAnimations(R.anim.slide_in_top, R.anim.slide_out_bottom);
ft.replace(bottomMenuContainerResource1, fragment1b);
fragment2c = Fragment2c.newInstance(this,
userManager == null || !userManager.isLoggedIn() ? getString(R.string.loginText) : userManager.getUserFullName());
ft.setCustomAnimations(R.anim.slide_in_bottom, R.anim.slide_out_top);
ft.replace(bottomMenuContainerResource2, fragment2c);
}
ft.commitAllowingStateLoss();
这里是slide_in_bottom.xml:
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="400"
android:fromYDelta="100%"
android:toYDelta="0%" />
如果需要,我可以提供更多信息。
答案 0 :(得分:1)
更改动画文件的代码
<强> slide_in_bottom.xml 强>
<?xml version="1.0" encoding="utf-8"?>
<set>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:propertyName="translationY"
android:valueType="floatType"
android:valueFrom="1280"
android:valueTo="0"
android:duration="1000"/>
</set>
<强> slide_out_top.xml 强>
<?xml version="1.0" encoding="utf-8"?>
<set>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:propertyName="translationY"
android:valueType="floatType"
android:valueFrom="0"
android:valueTo="-1280"
android:duration="1000"/>
</set>