我遇到了这个问题。我正在用滑动切换片段。
向左滑动:
public void nextFragment() {
if(collection.iterator(). hasNext()){
String fragmentNameToHide = collection.iterator().current();
String fragmentNameToShow = collection.iterator().next();
Fragment fragmentToShow = null;
if((fragmentToShow=fragmentManager.findFragmentByTag(fragmentNameToShow))==null){
Class<?> className=null;
try {
className = Class.forName(fragmentNameToShow);
Constructor<?> constructor = className.getConstructor();
Object object = constructor.newInstance();
fragmentToShow = (Fragment) object;
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.setCustomAnimations(R.animator.slide_in_right, R.animator.slide_out_left);
Fragment fragmentToHide = fragmentManager.findFragmentByTag(fragmentNameToHide);
ft.add(R.id.fragment_container, fragmentToShow, fragmentNameToShow);
ft.show(fragmentToShow);
ft.hide(fragmentToHide);
ft.commitAllowingStateLoss();
}
}
向右滑动:
public boolean prevFragment() {
if(collection.iterator().isLast()){
Log.i("prv", "son");
String fragmentNameToRemove = collection.iterator().current();
String fragmentNameToShow = collection.getFragmentList().get(0);
Fragment fragmentToShow = fragmentManager.findFragmentByTag(fragmentNameToShow);
Fragment fragmentToRemove2 = fragmentManager.findFragmentByTag(collection.getFragmentList().get(1));
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.setCustomAnimations(R.animator.slide_in_left, R.animator.slide_out_right);
Fragment fragmentToRemove = fragmentManager.findFragmentByTag(fragmentNameToRemove);
ft.hide(fragmentToRemove);
ft.hide(fragmentToRemove2);
ft.show(fragmentToShow);
ft.commit();
collection.iterator().setPosition(0);
Delict.getInstance().releaseDelict();
return false;
}else{
Log.i("prv", "son değil");
if (collection.iterator().hasPrev()) {
String fragmentNameToRemove = collection.iterator().current();
String fragmentNameToShow = collection.iterator().prev();
Fragment fragmentToShow = fragmentManager.findFragmentByTag(fragmentNameToShow);
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.setCustomAnimations(R.animator.slide_in_left, R.animator.slide_out_right);
Fragment fragmentToRemove = fragmentManager.findFragmentByTag(fragmentNameToRemove);
ft.show(fragmentToShow);
ft.hide(fragmentToRemove);
ft.commit();
return true;
}else{
android.os.Process.killProcess(android.os.Process.myPid());
return false;
}
}
}
以下是我的代码: 我的主要活动:
public class DelictActivity extends Activity implements SimpleGestureListener {
private SimpleGestureFilter detector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_delict);
detector = new SimpleGestureFilter(this,this, getFragmentManager());
if(getResources().getConfiguration().orientation==Configuration.ORIENTATION_PORTRAIT){
Fragment fragment = null;
FragmentTransaction ft = getFragmentManager().beginTransaction();
if((fragment=getFragmentManager().findFragmentByTag("com.example.PlateFragment"))==null){
PlateFragment plate = new PlateFragment();
ft.add(R.id.fragment_container, plate, "com.example.PlateFragment");
ft.show(plate);
}else{
ft.show(fragment);
}
ft.commit();
}
}
@Override
public boolean dispatchTouchEvent(MotionEvent me){
this.detector.onTouchEvent(me);
return super.dispatchTouchEvent(me);
}
}
我的第一个片段:
public class PlateFragment extends Fragment {
public PlateFragment() {
}
Button enterPlate;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_plate, null);
enterPlate = (Button) view.findViewById(R.id.openPlateLayer);
enterPlate.setOnClickListener(openPlateLayerListener);
return view;
}
OnClickListener openPlateLayerListener = new OnClickListener() {
@Override
public void onClick(View v) {
PlateDialogFragment newFragment = PlateDialogFragment.newInstance();
newFragment.addListener(dialogFragmentListener);
newFragment.show(getFragmentManager(),"dialog");
}
};
DialogFragmentListener dialogFragmentListener = new DialogFragmentListener() {
@Override
public void onSuccess() {
Delict delict =Delict.getInstance();
enterPlate.setText(delict.getPlate());
}
};
}
我的第二个片段:
public class CategoryFragment extends Fragment {
public CategoryFragment() {
}
TextView text;
View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_category, null);
Button categoryLayerButton = (Button) view.findViewById(R.id.selectCategory);
categoryLayerButton.setOnClickListener(categoryLayerButtonListener);
text = (TextView) view.findViewById(R.id.text);
return view;
}
OnClickListener categoryLayerButtonListener = new OnClickListener() {
@Override
public void onClick(View v) {
CategoryDialogFragment newFragment = CategoryDialogFragment.newInstance();
newFragment.addListener(dialogFragmentListener);
newFragment.show(getFragmentManager(),"dialog");
}
};
DialogFragmentListener dialogFragmentListener = new DialogFragmentListener() {
@Override
public void onSuccess() {
Delict delict = Delict.getInstance();
if(delict.getCategoryList().size()>1){
text.setText("ihlallerini yaptı;");
}else if(delict.getCategoryList().size()==1){
text.setText("ihlalini yaptı;");
}
loadCategoryImages();
}
};
private void loadCategoryImages() {
view.findViewById(R.id.selectCategory).setVisibility(View.GONE);
LinearLayout stack = (LinearLayout) view.findViewById(R.id.categoryLayout);
Delict delict = Delict.getInstance();
for(int i = 0; i<delict.getCategoryList().size(); i++){
ImageView image = new ImageView(getActivity());
image.setBackgroundResource(R.drawable.ic_launcher);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.setMargins((-30*i), 0, 0, 0);
image.setLayoutParams(params);
stack.addView(image);
}
}
private static final String FRAGMENT_KEY = "fragmentkey";
@Override
public void onSaveInstanceState(Bundle outState) {
outState.putBoolean(FRAGMENT_KEY, true);
super.onSaveInstanceState(outState);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (savedInstanceState != null) {
if(savedInstanceState.getBoolean(FRAGMENT_KEY)){
FragmentTransaction ft = getFragmentManager().beginTransaction();
Fragment frag1 = getFragmentManager().findFragmentByTag("com.example.PlateFragment");
ft.hide(frag1);
ft.commitAllowingStateLoss();
}
}
}
}
LogCat说:
03-13 15:01:43.320: E/AndroidRuntime(8477): FATAL EXCEPTION: main
03-13 15:01:43.320: E/AndroidRuntime(8477): java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
03-13 15:01:43.320: E/AndroidRuntime(8477): at android.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1265)
03-13 15:01:43.320: E/AndroidRuntime(8477): at android.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1276)
03-13 15:01:43.320: E/AndroidRuntime(8477): at android.app.BackStackRecord.commitInternal(BackStackRecord.java:541)
03-13 15:01:43.320: E/AndroidRuntime(8477): at android.app.BackStackRecord.commit(BackStackRecord.java:525)
03-13 15:01:43.320: E/AndroidRuntime(8477): at com.example.fragmentmanager.FragmentCollection.prevFragment(FragmentCollection.java:77)
03-13 15:01:43.320: E/AndroidRuntime(8477): at com.example.SimpleGestureFilter.onFling(SimpleGestureFilter.java:130)
03-13 15:01:43.320: E/AndroidRuntime(8477): at android.view.GestureDetector.onTouchEvent(GestureDetector.java:579)
03-13 15:01:43.320: E/AndroidRuntime(8477): at com.example.SimpleGestureFilter.onTouchEvent(SimpleGestureFilter.java:53)
03-13 15:01:43.320: E/AndroidRuntime(8477): at com.example.activities.DelictActivity.dispatchTouchEvent(DelictActivity.java:45)
03-13 15:01:43.320: E/AndroidRuntime(8477): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1897)
03-13 15:01:43.320: E/AndroidRuntime(8477): at android.view.View.dispatchPointerEvent(View.java:5726)
03-13 15:01:43.320: E/AndroidRuntime(8477): at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:2922)
03-13 15:01:43.320: E/AndroidRuntime(8477): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2498)
03-13 15:01:43.320: E/AndroidRuntime(8477): at android.view.ViewRootImpl.processInputEvents(ViewRootImpl.java:864)
03-13 15:01:43.320: E/AndroidRuntime(8477): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2507)
03-13 15:01:43.320: E/AndroidRuntime(8477): at android.os.Handler.dispatchMessage(Handler.java:99)
03-13 15:01:43.320: E/AndroidRuntime(8477): at android.os.Looper.loop(Looper.java:137)
03-13 15:01:43.320: E/AndroidRuntime(8477): at android.app.ActivityThread.main(ActivityThread.java:4447)
03-13 15:01:43.320: E/AndroidRuntime(8477): at java.lang.reflect.Method.invokeNative(Native Method)
03-13 15:01:43.320: E/AndroidRuntime(8477): at java.lang.reflect.Method.invoke(Method.java:511)
03-13 15:01:43.320: E/AndroidRuntime(8477): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-13 15:01:43.320: E/AndroidRuntime(8477): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-13 15:01:43.320: E/AndroidRuntime(8477): at dalvik.system.NativeStart.main(Native Method)
第三个片段是这样的。如您所见,在活动中的onCreate
方法中,第一个片段被添加到容器中。我有2 xml的主要活动。第一个用于纵向模式,另一个用于横向模式。如果方向为纵向,则屏幕方向会更改,其中一个片段将显示在屏幕上。如果方向是横向的,则应始终使用相同的布局 - 用于活动的横向模式的布局。
但是在第二次活动后屏幕方向改变后滑动,我收到此错误。有人可以帮帮我吗?
答案 0 :(得分:0)
您是否在清单文件中定义了这样的活动?如果由于重新创建激活而没有,则可能会出现此类错误。
机器人:configChanges = “keyboardHidden |取向|屏幕尺寸”