gpt4 book ai didi

安卓错误 : "commit already called" when remove a fragment

转载 作者:太空狗 更新时间:2023-10-29 15:34:56 26 4
gpt4 key购买 nike

我想实现一个有 fragment 的 Activity !当我点击 Fragment1 时, Fragment2 被调用,当我点击 Fragment2 时, Fragment2 应该从屏幕上删除!我通过在其 onCreateView 中调用 Fragment2 的 LinearLayout 的 setOnclickListener 来实现它,在我的 onclicklistener 上,我调用了

transaction.remove(myFragment);

transaction.commit();

但在那之后我遇到了这个错误:commit already called我该如何解决这个错误,这是我的代码:这是我的 fragment 类

public class ArticleFragment extends Fragment {
final static String ARG_POSITION = "position";
int mCurrentPosition = -1;
private static LinearLayout l;
private android.support.v4.app.FragmentTransaction transaction;
private Fragment newFragment;

public void setArticleFragment(android.support.v4.app.FragmentTransaction transaction , Fragment newFragment) {
this.transaction = transaction;
this.newFragment = newFragment;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (savedInstanceState != null) {
mCurrentPosition = savedInstanceState.getInt(ARG_POSITION);
}
View v = inflater.inflate(R.layout.article_view, container , false);
l = (LinearLayout) v.findViewById(R.id.transparentArea);
if(l == null)
l.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
transaction.remove(newFragment);
transaction.commit();
}
});
return v;
}
}

和我的 mainAcitivity 类

public class MainActivity extends FragmentActivity implements
HeadlinesFragment.OnHeadlineSelectedListener{

HeadlinesFragment firstFragment;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.news_articles);
if (findViewById(R.id.fragment_container) != null) {
if (savedInstanceState != null) {
return;
}
firstFragment = new HeadlinesFragment();
firstFragment.setArguments(getIntent().getExtras());
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, firstFragment).commit();
}
}

public void onArticleSelected(int position) {
ArticleFragment articleFrag = (ArticleFragment) getSupportFragmentManager()
.findFragmentById(R.id.article_fragment);

if (articleFrag != null) {
articleFrag.updateArticleView(position);

} else {
final ArticleFragment newFragment = new ArticleFragment();
Bundle args = new Bundle();
args.putInt(ArticleFragment.ARG_POSITION, position);
newFragment.setArguments(args);

final FragmentTransaction transaction = getSupportFragmentManager()
.beginTransaction();
newFragment.setArticleFragment(transaction, newFragment);

transaction.setCustomAnimations(R.anim.slidein, R.anim.slideout);
transaction.add(R.id.fragment_container, newFragment);
transaction.commit();

}
}

最佳答案

不是重用传入的事务,而是创建一个新的 FragmentTransaction 实例来删除 Fragment2。

更简单的方法是将第一个 fragment 事务添加到 fragment 返回堆栈(例如 addToBackStack(null)),然后在 Fragment2 中,只需使用 FragmentManager 弹出返回堆栈 popBackStack()

关于安卓错误 : "commit already called" when remove a fragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18697490/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com