当前位置: 首页 > news >正文

android app 一个 crash的解决过程!

一、日志:
crash
2024-10-25 12:15:33.020  2113-2113  AndroidRuntime          pid-2113                             E  FATAL EXCEPTION: main
Process: com..workhome, PID: 2113
java.lang.RuntimeException: Unable to start activity ComponentInfo{com..workhome/com.workhome.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'androidx.lifecycle.LiveData com.kingtop.workhome.room.AppRepository.getAllApps()' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3484)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3648)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2116)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:223)
    at android.app.ActivityThread.main(ActivityThread.java:7723)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:612)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:997)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'androidx.lifecycle.LiveData com.workhome.room.AppRepository.getAllApps()' on a null object reference
    at com.workhome.MainActivity.setData(MainActivity.java:528)
    at com.workhome.MainActivity.onCreate(MainActivity.java:457)
    at android.app.Activity.performCreate(Activity.java:8000)
    at android.app.Activity.performCreate(Activity.java:7984)    
中文回复,难道mAppRepository 这个为空?
是因为private void initRoom() {
        Log.d(TAG, "initRoom: Starting");
        new Thread(() -> {
            AppDatabase db = AppDatabase.getInstance(this);
            AppInfoDao dao = db.appInfoDao();
            mAppRepository = new AppRepository(dao);
            runOnUiThread(() -> Log.d(TAG, "initRoom: Room database initialized"));
        }).start();
        Log.d(TAG, "initRoom: Thread started");
    }

二、提问:开了一个线程在初始化,就是还没初始化完成,就立马调用mAppRepository导致?

三、解决办法:可以把 setData() 的调用移动到初始化完成的回调中(UI线程)
     private void initRoom() {
        Log.d(TAG, "initRoom: Starting");
        new Thread(() -> {
            AppDatabase db = AppDatabase.getInstance(this);
            AppInfoDao dao = db.appInfoDao();
            mAppRepository = new AppRepository(dao);
//            runOnUiThread(() -> Log.d(TAG, "initRoom: Room database initialized"));

            runOnUiThread(() -> {
                Log.d(TAG, "initRoom: Room database initialized");
                setData(); // ← 初始化完成后再调用
            });

        }).start();
        Log.d(TAG, "initRoom: Thread started");
    }
 


文章转载自:

http://YKjbzqeM.mxdhy.cn
http://ChTUcSlr.mxdhy.cn
http://HmRPm6Af.mxdhy.cn
http://SpB77k2F.mxdhy.cn
http://pUTH7XlG.mxdhy.cn
http://SAqyLrRg.mxdhy.cn
http://SGUjCg15.mxdhy.cn
http://Fmlzx3N8.mxdhy.cn
http://ISJqQOrv.mxdhy.cn
http://3rp7gzdY.mxdhy.cn
http://YAAtRls1.mxdhy.cn
http://SG2ALi4S.mxdhy.cn
http://ig1QHvAR.mxdhy.cn
http://ok7CNNC0.mxdhy.cn
http://NndnFayg.mxdhy.cn
http://g2PHekyV.mxdhy.cn
http://picf8l0N.mxdhy.cn
http://ih95iB8n.mxdhy.cn
http://G1r7uoY4.mxdhy.cn
http://rS0Tfimo.mxdhy.cn
http://vWgU8WH4.mxdhy.cn
http://8VW7iVEh.mxdhy.cn
http://E7SQozzV.mxdhy.cn
http://zE1GZAzZ.mxdhy.cn
http://lobtmt8J.mxdhy.cn
http://FsF2rW0D.mxdhy.cn
http://9kzApNBG.mxdhy.cn
http://G70QHE2P.mxdhy.cn
http://pwmky2d1.mxdhy.cn
http://oUjvDF14.mxdhy.cn
http://www.dtcms.com/a/235687.html

相关文章:

  • 26.【新型数据架构】-零ETL架构
  • python打卡day46@浙大疏锦行
  • 【驱动】Orin NX恢复备份失败:does not match the current board you‘re flashing onto
  • 数列运算中的常见错因分析
  • java学习电子书推荐
  • 密码学基础——SM4算法
  • 408第一季 - 数据结构 - 栈与队列的应用
  • Cesium等高线
  • 【GESP真题解析】第 14 集 GESP 三级 2024 年 9 月编程题 1:平衡序列
  • Python打卡第46天
  • 猜字符位置游戏-position gasses
  • STM32标准库-TIM输出比较
  • SAP ABAP LOOP GROUP BY 分组循环的使用方法
  • LMG1020YFFR 电子元器件详解
  • 贝叶斯深度学习!华科大《Nat. Commun.》发表BNN重大突破!
  • 如何计算光伏工程造价预算表?
  • Linux驱动:class_create、device_create
  • 爱普生研发全新恒温晶体振荡器 “省、小、精”加速通信产业释放新质动能!
  • Dubbo学习(一):Dubbo介绍
  • 韩国IKS特价型号找游工IKS-LM-SN1G交换机
  • SpringMVC执行流程
  • Odoo 19 路线图(新功能)
  • C语言字符数组输入输出方法大全(附带实例)
  • Python训练day40
  • API标准的本质与演进:从 REST 架构到 AI 服务集成
  • DeepSeek 赋能智慧能源:微电网优化调度的智能革新路径
  • 双碳时代,能源调度的难题正从“发电侧”转向“企业侧”
  • LeetCode--23.合并k个升序链表
  • 【计算机网络】非阻塞IO——select实现多路转接
  • 量化面试绿皮书:5. 扑克牌游戏概率与期望值