Android Studio - Error: Commit already called
Android Studio - Error: Commit already called
我遇到了这个错误:
java.lang.IllegalStateException: commit already called
at androidx.fragment.app.BackStackRecord.commitInternal(BackStackRecord.java:669)
at androidx.fragment.app.BackStackRecord.commit(BackStackRecord.java:637)
at com.example.ui.PACK.SUBPACK.subpack.onClick(subpack.java:94)
at android.view.View.performClick(View.java:6600)
at android.view.View.performClickInternal(View.java:6577)
at android.view.View.access00(View.java:781)
at android.view.View$PerformClick.run(View.java:25912)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6923)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:870)
这是我的JAVA代码:
public class subpack extends Fragment {
private FragmentTransaction ft;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_subpack, container, false);
ft = getFragmentManager().beginTransaction();
//TXT LUNES EVENT LISTENER
txt_lunes.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
fragment = new lunes_gestionar();
ft.replace(R.id.fragment,fragment).setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN).commit();
}
});
//TXT MARTES EVENT LISTENER
txt_martes.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
fragment = new martes_gestionar();
ft.replace(R.id.fragment,fragment).setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN).commit();
}
});
描述:
我有一个片段,里面有另一个片段,其中 TEXT VIEWS(星期一和星期二)用于通过 Fragment Transaction 更改片段。问题是当我第二次单击此文本视图时。
我该如何解决这个问题?
感谢阅读!
只有 1 个 beginTransaction() 是不正确的,您应该在每次替换之前使用 beginTransaction()。
使用这个:
getFragmentManager().beginTransaction().replace() ...
试试这个:
txt_lunes.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
fragment = new lunes_gestionar();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fragment,fragment)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN).commit();
}
});
txt_martes.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
fragment = new martes_gestionar();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fragment,fragment)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN).commit();
}
});
我遇到了这个错误:
java.lang.IllegalStateException: commit already called
at androidx.fragment.app.BackStackRecord.commitInternal(BackStackRecord.java:669)
at androidx.fragment.app.BackStackRecord.commit(BackStackRecord.java:637)
at com.example.ui.PACK.SUBPACK.subpack.onClick(subpack.java:94)
at android.view.View.performClick(View.java:6600)
at android.view.View.performClickInternal(View.java:6577)
at android.view.View.access00(View.java:781)
at android.view.View$PerformClick.run(View.java:25912)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6923)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:870)
这是我的JAVA代码:
public class subpack extends Fragment {
private FragmentTransaction ft;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_subpack, container, false);
ft = getFragmentManager().beginTransaction();
//TXT LUNES EVENT LISTENER
txt_lunes.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
fragment = new lunes_gestionar();
ft.replace(R.id.fragment,fragment).setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN).commit();
}
});
//TXT MARTES EVENT LISTENER
txt_martes.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
fragment = new martes_gestionar();
ft.replace(R.id.fragment,fragment).setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN).commit();
}
});
描述:
我有一个片段,里面有另一个片段,其中 TEXT VIEWS(星期一和星期二)用于通过 Fragment Transaction 更改片段。问题是当我第二次单击此文本视图时。
我该如何解决这个问题?
感谢阅读!
只有 1 个 beginTransaction() 是不正确的,您应该在每次替换之前使用 beginTransaction()。
使用这个:
getFragmentManager().beginTransaction().replace() ...
试试这个:
txt_lunes.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
fragment = new lunes_gestionar();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fragment,fragment)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN).commit();
}
});
txt_martes.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
fragment = new martes_gestionar();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fragment,fragment)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN).commit();
}
});