Fragment問題:java.lang.IllegalStateException

錯誤提示:?

?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();}}
}

?

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/bicheng/14473.shtml
繁體地址,請注明出處:http://hk.pswp.cn/bicheng/14473.shtml
英文地址,請注明出處:http://en.pswp.cn/bicheng/14473.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Vue速成學習筆記

這兩天速成了一下Vue,在這里記錄一下相關的筆記,之后有時間詳細學Vue的時候再來回顧一下! 一、Vue理解 1、Vue的核心特征:雙向綁定。 在網頁中,存在視圖和數據。在Vue之前,需要使用JavaScript編寫復雜的邏…

web前端面試題

web前端面試題 1、前端如何實現優化性能 (1)減少網絡時間 ①使用DNS緩存技術 ? ②減少需要傳輸的文件尺寸 ? ③加快文件傳輸速度 (2)減少發送的請求數量 ①利用瀏覽器緩存 ? ②使用合并的圖片文件 (3)提高瀏覽器下載的并發度 ①JS文件放在HTML文檔最后 ? ②使用多個域名 (…

音視頻及H264/H256編碼相關原理

一、音視頻封裝格式原理: 我們播放的視頻文件一般都是用一種封裝格式封裝起來的,封裝格式的作用是什么呢?一般視頻文件里不光有視頻,還有音頻,封裝格式的作用就是把視頻和音頻打包起來。 所以我們先要解封裝格式&#…

谷歌上架,個人號比企業號好上?“14+20”封測如何解決,你知道了嗎

在Google Play上架應用,對開發者而言,既是挑戰也是機遇。隨著谷歌政策的不斷更新,特別是要求2023年11月13日后注冊的個人開發者賬號在發布正式版應用前,必須經過20人連續14天的封閉測試。 這一政策的改變使得許多開發者開始考慮使…

什么是物聯網通信網關?-天拓四方

在信息化、智能化的時代,物聯網技術的廣泛應用正在逐漸改變我們的生活方式。物聯網通過各種傳感器和設備,將現實世界與數字世界緊密相連,從而實現智能化、自動化的生活和工作方式。作為物聯網生態系統中的重要組成部分,物聯網通信…

【數據結構】堆(Heep)

???專欄:數據結構 🧑?🎓個人主頁:SWsunlight 目錄 一、堆: 定義: 性質: 大、小根堆: 二、實現堆(完全二叉樹): 前言: …

四、Filter

Filter簡介 Filter 的基本功能是對Servlet容器調用Servlet的過程進行攔截,從而在Servlet進行響應處理的前后實現一些特殊的功能.在Servlet API 中定義了三個接口類來供開發人員編寫Filter 程序:Filter,FilterChain,FilterConfigFilter 程序是一個實現了…

Spring:Spring事務失效的各種場景以及解決方法

一、前言 Spring事務是指Spring框架中提供的事務管理功能,它可以幫助開發者簡化事務管理的復雜性,提高代碼的可維護性和可擴展性。本文將總結并分析Spring事務失效的各種場景,幫助你全面了解事務失效的原因和解決方案。 二、Spring事務失效的…

51匯編--數碼管顯示

;將內部RAM30H~32H單元中存儲的6位十進制數顯示在6個數碼管上。 ;要求編寫將一個內存單元中的壓縮BCD碼轉換為兩個七段顯示碼的子程序和 ;延時子程序。不允許在程序中給30H、31H和32H單元賦值,要通過存 ;儲器窗口賦值。嘗試改變點亮數碼管的時間&#xf…

Linux軟硬鏈接及動靜態庫

軟硬鏈接與動靜態庫 軟連接 創建鏈接的方法: ln -s test1.txt test2.txt 其中ln 是link(鏈接),-s 是soft(軟),后者鏈接前者。 此時打開test2.txt,發現其中內容與test.txt一致。那么軟連接到底建立了什么聯系?…

輕松購物,盡在購物網

在忙碌的生活中,想要找到心儀的商品,卻總是苦于沒有時間和精力去實體店挑選?別擔心,購物網為您提供一站式的購物體驗。無論是時尚服飾、家居用品,還是美食特產,這里都能滿足您的需求。只需輕輕一點&#xf…

監聽element-ui表格滾動事件

當element-ui表格高度寫死之后,表格內容超出高度就會在右側顯示滾動條,監聽滾動事件 首先給表格加ref,ref"refTable" 然后在mounted生命周期里寫監聽事件 mounted() {this.$refs.refTable.$el.onwheel (e) > {console.log(滾…

深入解析線程上下文切換的原理與優化策略

深入解析線程上下文切換的原理與優化策略 定義觸發條件線程上下文切換的過程線程上下文切換的開銷減少上下文切換的方法示例代碼總結 線程上下文切換(Thread Context Switch)是操作系統調度機制的重要組成部分。它涉及保存當前線程的狀態并恢復新線程的狀…

vue中使用ant的rangePicker設置禁選時間和時間格式

<a-range-pickerstyle"width: 100%":disabled-date"disabledDate"v-model:value"time"valueFormat"YYYY-MM-DD" />valueFormat設置時間格式YYYY-MM-DD 通過dayjs獲取時間&#xff0c;return過濾后的時間 const disabledDate (…

安裝apex時遇到的問題

Apex是混合精度庫&#xff0c;安裝過程中常常出現各種問題&#xff0c;在此記錄一下 首先&#xff0c;不能使用pip install apex,這是兩個完全不同的庫&#xff0c;需要去官網下載 其次&#xff0c;參考官網安裝時可能會報錯&#xff1a;could not build wheels for apex, whic…

C/C++運行時庫和UCRT系統通用運行時庫總結及問題實例分享

目錄 1、概述 2、不同版本的Visual Studio對應的運行時庫說明 3、在Windbg10.0安裝目錄中獲取UCRT通用運行時庫 4、微軟官網對UCRT通用運行時庫的相關說明 5、使用Visual Studio 2017開發軟件初期遇到的UCRT通用運行時庫問題 6、如何查看軟件依賴了哪些C/C運行時庫&#…

后端雪花算法主鍵ID傳到前端變了

Mybatis Plus 的主鍵策略&#xff1a; /*** id*/TableId(type IdType.ASSIGN_ID)private Long id; 這個主鍵策略會用雪花算法生成一個 19位的ID&#xff0c;比如 1791006670084734978 現象 后端生成的 id 是正常的&#xff0c;通過 swagger 文檔此時獲取到的 id 也和數據庫中…

leetcode-盛水最多的容器-109

題目要求 思路 1.正常用雙循環外循環i從0開始&#xff0c;內循環從height.size()-1開始去計算每一個值是可以的&#xff0c;但是因為數據量太大&#xff0c;會超時。 2.考慮到超時&#xff0c;需要優化一些&#xff0c;比如第一個選下標1&#xff0c;第二個選下標3和第一個選下…

Java 面試題日常練習

### 基礎知識 1. **什么是 JVM&#xff1f;解釋其架構。** - JVM&#xff08;Java Virtual Machine&#xff09;是 Java 程序的運行時環境。其架構包括類加載器子系統、運行時數據區&#xff08;堆、棧、本地方法棧、PC 寄存器、方法區&#xff09;、執行引擎和本地方法接口…

心識宇宙 x TapData:如何加速落地實時數倉,助力 AI 企業智慧決策

使用 TapData&#xff0c;化繁為簡&#xff0c;擺脫手動搭建、維護數據管道的諸多煩擾&#xff0c;輕量代替 OGG、DSG 等同步工具&#xff0c;「CDC 流處理 數據集成」組合拳&#xff0c;加速倉內數據流轉&#xff0c;幫助企業將真正具有業務價值的數據作用到實處&#xff0c…