錯誤提示:?
?Process: com.example.accountapp, PID: 3987
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? java.lang.IllegalStateException: Could not execute method for android:onClick
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? ?at androidx.appcompat.app
?Caused by: java.lang.IllegalStateException: commit already called
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? ?at androidx.fragment.app.BackStackRecord.commitInternal(BackStackRecord.java:315)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? ?at androidx.fragment.app.BackStackRecord.commit(BackStackRecord.java:294)?
這是一個Android應用程序中的嚴重錯誤,其中涉及到"java.lang.IllegalStateException"異常。具體來說,錯誤信息是"Could not execute method for android:onClick",并且也提到了"commit already called"的問題。這種錯誤通常出現在Fragment的事務提交上,可能是由于重復調用了commit方法導致的。?
原因:
在Android中,每個?FragmentTransaction
?對象只能提交一次。一旦調用了?commit()
?方法提交一個事務,該事務就被提交到活動的?FragmentManager
?中,之后再次調用?commit()
?方法會導致 "Can not perform this action after onSaveInstanceState" 異常或者 "commit already called" 異常。這是因為?commit()
?方法已經被調用,再次調用會導致不可預測的行為。
原有代碼:
package com.example.accountapp.pages;import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentContainerView;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;import android.os.Bundle;
import android.view.View;
import android.widget.TextView;import com.example.accountapp.R;
import com.example.accountapp.fragment.addaccount.AddExchangeFragment;
import com.example.accountapp.fragment.addaccount.AddInFragment;
import com.example.accountapp.fragment.addaccount.AddOutFragment;public class AddAccount extends AppCompatActivity {private int tabIndex = 1; //當前選中的tab頁private TextView tab1,tab2,tab3,cancelTxt;private View divider1,divider2;private FragmentContainerView fragmentContainerView;private FragmentManager fragmentManager;private FragmentTransaction fragmentTransaction;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_add_account);initView();// 動態加載 FragmentFragmentManager fragmentManager = getSupportFragmentManager();FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();//開啟事務fragmentTransaction.add(R.id.icon_frag,new AddOutFragment());fragmentTransaction.commit();}// 切換 tab頁 點擊方法public void tabChange(View view) {int index = view.getId();if(index == R.id.tab1){System.out.println("支出");divider1.setVisibility(View.INVISIBLE);divider2.setVisibility(View.VISIBLE);tab1.setBackgroundResource(R.drawable.selec_tab);tab3.setBackgroundResource(0);tab2.setBackgroundResource(0);fragmentTransaction.replace(R.id.icon_frag,new AddOutFragment());}else if(index == R.id.tab2){System.out.println("收入");divider1.setVisibility(View.INVISIBLE);divider2.setVisibility(View.INVISIBLE);tab2.setBackgroundResource(R.drawable.selec_tab);tab1.setBackgroundResource(0);tab3.setBackgroundResource(0);FragmentManager fragmentManager = getSupportFragmentManager();FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();//開啟事務fragmentTransaction.replace(R.id.icon_frag,new AddInFragment());}else{System.out.println("轉賬");divider1.setVisibility(View.VISIBLE);divider2.setVisibility(View.INVISIBLE);tab3.setBackgroundResource(R.drawable.selec_tab);tab1.setBackgroundResource(0);tab2.setBackgroundResource(0);fragmentTransaction.replace(R.id.icon_frag,new AddExchangeFragment());}fragmentTransaction.commit();}
}
修改過后:
每次創建新的?FragmentTransaction
?對象
package com.example.accountapp.pages;import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentContainerView;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;import android.os.Bundle;
import android.view.View;
import android.widget.TextView;import com.example.accountapp.R;
import com.example.accountapp.fragment.addaccount.AddExchangeFragment;
import com.example.accountapp.fragment.addaccount.AddInFragment;
import com.example.accountapp.fragment.addaccount.AddOutFragment;public class AddAccount extends AppCompatActivity {private int tabIndex = 1; //當前選中的tab頁private TextView tab1,tab2,tab3,cancelTxt;private View divider1,divider2;private FragmentContainerView fragmentContainerView;private FragmentManager fragmentManager;private FragmentTransaction fragmentTransaction;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_add_account);initView();// 動態加載 FragmentFragmentManager fragmentManager = getSupportFragmentManager();FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();//開啟事務fragmentTransaction.add(R.id.icon_frag,new AddOutFragment());fragmentTransaction.commit();}// 切換 tab頁 點擊方法public void tabChange(View view) {int index = view.getId();if(index == R.id.tab1){System.out.println("支出");divider1.setVisibility(View.INVISIBLE);divider2.setVisibility(View.VISIBLE);tab1.setBackgroundResource(R.drawable.selec_tab);tab3.setBackgroundResource(0);tab2.setBackgroundResource(0);FragmentManager fragmentManager = getSupportFragmentManager();FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();//開啟事務fragmentTransaction.replace(R.id.icon_frag,new AddOutFragment());fragmentTransaction.commit();}else if(index == R.id.tab2){System.out.println("收入");divider1.setVisibility(View.INVISIBLE);divider2.setVisibility(View.INVISIBLE);tab2.setBackgroundResource(R.drawable.selec_tab);tab1.setBackgroundResource(0);tab3.setBackgroundResource(0);FragmentManager fragmentManager = getSupportFragmentManager();FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();//開啟事務fragmentTransaction.replace(R.id.icon_frag,new AddInFragment());fragmentTransaction.commit();}else{System.out.println("轉賬");divider1.setVisibility(View.VISIBLE);divider2.setVisibility(View.INVISIBLE);tab3.setBackgroundResource(R.drawable.selec_tab);tab1.setBackgroundResource(0);tab2.setBackgroundResource(0);FragmentManager fragmentManager = getSupportFragmentManager();FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();//開啟事務fragmentTransaction.replace(R.id.icon_frag,new AddExchangeFragment());fragmentTransaction.commit();}}
}
?