添加片段破坏活动
我有一个活动,容器是一个FrameLayout。我需要为该容器添加一个片段,但这样会抛出IllegalStateException,从而破坏我的Activity。添加片段破坏活动
这是我在活动布局容器:
<FrameLayout
android:id="@+id/seeMoreContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
这是我的片段添加到活动
PlacesSeeMoreFragment placesSeeMoreFragment = new PlacesSeeMoreFragment();
Bundle bundle = new Bundle();
bundle.putInt(PlacesSeeMoreFragment.KEY, poiID);
placesSeeMoreFragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction().add(R.id.seeMoreContainer,
placesSeeMoreFragment).commit();
这是logcat的
java.lang.IllegalStateException: Activity has been destroyed
at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1515)
at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:634)
at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:613)
at net.ilb.Activities.PlacesActivity.openPlaceSeeMore(PlacesActivity.java:140)
什么我在这里做错了吗?
编辑:
我的片段从一个公共方法,这是我从一个ListView
的onItemClickListener调用添加到活动这是整个方法
public void openPlaceSeeMore(int poiID){
PlacesSeeMoreFragment placesSeeMoreFragment = new PlacesSeeMoreFragment();
Bundle bundle = new Bundle();
bundle.putInt(PlacesSeeMoreFragment.KEY, poiID);
placesSeeMoreFragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction().add(R.id.seeMoreContainer, placesSeeMoreFragment).commit();
}
这是怎么该方法被称为
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
PlacesActivity placesActivity = new PlacesActivity();
placesActivity.openPlaceSeeMore(poi.getId());
}
});
我猜你是调用此:
PlacesActivity placesActivity = new PlacesActivity();
这是非常错误的做法来调用活动声明的方法。 每次单击列表上的某个内容时,它都会创建一个新的活动实例。另外,如果你想捕获列表点击,你应该使用一个接口,而不是在任何地方使用活动对象。
希望这会有所帮助。
使用下面的代码....
Fragment fragment = new PlacesSeeMoreFragment();
FragmentManager fm = getActivity().getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.content_frame, fragment);
ft.commit();
Bundle bundle = new Bundle();
bundle.putInt(PlacesSeeMoreFragment.KEY, poiID);
fragment.setArguments(bundle);
抛出同样的异常。 –
在代码中使用ActivityName.this而不是getActivity。 –
不幸的是,使用这样做并没有改变任何东西。 –
有时我面临同样的问题,这就是我所做的。使用commitAllowingStateLoss()
而不是commit()
。试试这个
getSupportFragmentManager().beginTransaction().add(R.id.seeMoreContainer, placesSeeMoreFragment).commitAllowingStateLoss();
即使使用commitAllowingStateLoss(),它仍会引发相同的异常。 –
试试这个,
PlacesSeeMoreFragment placesSeeMoreFragment = new PlacesSeeMoreFragment();
Bundle bundle = new Bundle();
bundle.putInt(PlacesSeeMoreFragment.KEY, poiID);
placesSeeMoreFragment.setArguments(bundle);
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction=manager.beginTransaction();
transaction.add(R.id.seeMoreContainer, placesSeeMoreFragment);
transaction.commit();
重写onDetach片段后,抛出相同的异常。它不应该捕获它并抛出RuntimeException吗? –
尝试更新答案。 – Omi
更新后的答案基本上是我在做的,尽管你定义了一个片段管理器和事务,而不是直接使用它们。 –
这是什么行PlacesActivity.java:140? – k0sh
@ k0sh这是这一行: getSupportFragmentManager()。beginTransaction()。add(R.id.seeMoreContainer, placesSeeMoreFragment).commit(); –