首页 > 其他分享 >android transaction failed 29201/-1, size 0-0 line 3009

android transaction failed 29201/-1, size 0-0 line 3009

时间:2023-07-19 14:03:33浏览次数:35  
标签:事务 transaction Fragment failed 29201 提交 Activity onCreate

解决 "android transaction failed 29201/-1, size 0-0 line 3009" 错误

引言

在Android开发中,我们经常会遇到各种错误和异常。其中一个常见的错误是 "android transaction failed 29201/-1, size 0-0 line 3009"。这个错误通常与Fragment事务相关,并且可能会导致应用崩溃或功能异常。本文将指导你如何解决这个错误,让你的应用恢复正常运行。

错误原因

这个错误通常发生在应用中使用Fragment进行页面切换或布局更改时。它意味着在进行Fragment事务时出现了问题,可能是由于以下原因之一:

  1. Fragment事务的提交发生在Activity的生命周期方法之外,例如onCreate()之后。
  2. Fragment事务的提交发生在异步线程中,而不是主线程。
  3. Fragment事务的提交发生在Fragment的状态已经保存时,例如在Activity的onSaveInstanceState()之后。

现在,让我们来解决这个问题并确保你的应用正常运行。

解决步骤

下面的表格列出了解决这个错误的步骤:

步骤 描述
步骤 1 确保Fragment事务的提交发生在Activity的生命周期方法之内。
步骤 2 确保Fragment事务的提交发生在主线程中。
步骤 3 避免在Fragment的状态已经保存时进行Fragment事务的提交。

接下来,我们将详细讨论每个步骤以及需要采取的具体措施。

步骤 1:确保Fragment事务的提交发生在Activity的生命周期方法之内

在Android中,Fragment事务应该在Activity的生命周期方法之内进行提交。这样可以确保Fragment事务与Activity的状态同步,并且不会出现异常。

下面是一个示例代码,展示了如何在Activity的onCreate()方法中进行Fragment事务的提交:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // 创建并初始化Fragment
    Fragment fragment = new MyFragment();
    
    // 开启Fragment事务
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    
    // 添加Fragment到容器中
    transaction.add(R.id.fragment_container, fragment);
    
    // 提交事务
    transaction.commit();
}

请注意,在这个示例代码中,Fragment事务的提交发生在Activity的onCreate()方法内部。

步骤 2:确保Fragment事务的提交发生在主线程中

在Android中,Fragment事务的提交必须发生在主线程中。这是因为Fragment事务会对UI进行修改,而UI只能在主线程中进行修改。

下面是一个示例代码,展示了如何在主线程中进行Fragment事务的提交:

runOnUiThread(new Runnable() {
    @Override
    public void run() {
        // 开启Fragment事务
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        
        // 添加Fragment到容器中
        transaction.add(R.id.fragment_container, fragment);
        
        // 提交事务
        transaction.commit();
    }
});

在这个示例代码中,我们使用了runOnUiThread()方法将Fragment事务的提交放在了主线程中。

步骤 3:避免在Fragment的状态已经保存时进行Fragment事务的提交

在Activity的生命周期方法onSaveInstanceState()被调用后,Fragment的状态已经保存。在这种情况下,进行Fragment事务的提交可能会导致异常。

为了避免这个问题,我们可以在Activity的onCreate()方法中检查savedInstanceState是否为null,只有当它为null时才进行Fragment事务的提交。

下面是一个示例代码,展示了如何避免在Fragment的状态已经保存时进行Fragment事务的提交:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // 检查savedInstanceState是否为null
    if (savedInstanceState == null) {
        // 创建并初始化Fragment
        Fragment fragment = new MyFragment();
        
        // 开启Fragment事务
        FragmentTransaction transaction =

标签:事务,transaction,Fragment,failed,29201,提交,Activity,onCreate
From: https://blog.51cto.com/u_16175477/6775351

相关文章

  • Makefile:162:recipe for target ‘xxx.o‘ failed!
    Q:使用makefile对工程进行编译的时候,出现指定报错:Makefile:162:recipefortarget‘xxx.o‘failed!A:该问题报错的意思是缺少依赖。根本问题是由于某个错误,导致过程文件xxx.o无法正常编译成功(makefile中最终目标文件的编译需要依赖过程目标文件xxx.o,而过程目标文件xxx.......
  • 针对el-menu-item组件的警告Invalid event arguments: event validation failed for e
    现象:解决办法:加上index这个唯一标识,不然会有意想不到的问题......
  • 1205 - Lock wait timeout exceeded; try restarting transaction
    参考:https://blog.csdn.net/qq_52466976/article/details/127808605报错是因为有表数据修改但是没提交,常见是Navicat上操作后没打勾提交导致的showFULLPROCESSLIST;显示正在操作的进程 解决:关掉这个操作的进程,根据ip定位一下 ......
  • Github:提交代码到Github上报错Git: fatal unable to access "***/: Failed to connect
    在国内网环境,使用VScode提交代码到Github上时,因为国的防火墙问题,经常会报错:Git:fatalunabletoaccess"https://github.com/***/***.io.git/:Failedtoconnecttogithub.comport443after21074ms:Couldn'tconnecttoserver 可能原因:出现该错误是因为使用了proxy......
  • ansible构建失败 scp transfer mechanism failed on **** Use ANSIBLE DEBUG=1\nto s
    ansible构建docker服务的失败排查经过(之前ansible构建成功)第一步:使用ansible对应ip/或者在/etc/ansible/hosts中配置的label-mping查看当前连接对应服务器状态对应失败服务器的连接状态*@**** |FAILED!=>{"failed":true,"msg":"failedtotransferfileto/ho......
  • 如何解决error: failed to push some refs to
    出现错误的主要原因是gitee(github)中的README.md文件不在本地代码目录中此时我们要执行gitpull--rebaseoriginmaster命令README.md拉到本地,然后执行gitpushoriginmaster ......
  • visual studio在运行ashx文件时 出现 Failed to load resource: the server responde
     报错详细信息如下“/”应用程序中的服务器错误。分析器错误说明: 在分析向此请求提供服务所需资源时出错。请检查下列特定分析错误详细信息并适当地修改源文件。分析器错误消息: 未能创建类型“WebApp.FileUploadHandler”。源错误:行1:<%@WebHandlerLanguage=......
  • maven打包repackage failed: Unable to find main class
    maven打包提示这个问题。原因:主项目pomxml文件中,不需要<build>打包的配置,只需要在有入口类的模块pom.xml配置好<build><build><finalName>${project.artifactId}</finalName><plugins><plugin><groupId>org.......
  • 【dRep报错】运行dRep去冗余时出现checkm failed的处理
    做宏基因组分析时,会用到drep软件去冗余,有时会出现checkMfailed的错误$dRepdereplicatedreplicated_out-gbins/*fa#运行命令错误信息如下:RunningcheckM!!!checkMfailed!!!官方文档提到了几个解决方案https://drep.readthedocs.io/en/latest/advanced_use.html......
  • [GIT]解决:failed to push some refs to ...(过程重现)
    本问题有很多种情况,解决方法也很多,本文只针对笔者本人的自身诉求和情况,选择了一种适合我的解决方法。仅供参考。1问题描述johnnyzen@XXDSSSMINGW64/e/source_code/BigData/bdp_common_data_service(develop)$gitpushTogitlab-bigdata.johnnyzen.cn:platform-software/b......