首页 > 解决方案 > 读取资产中的文件时应用程序在启动时崩溃

问题描述

我正在尝试从我的资产文件夹中的文件中接收 JSON 字符串,但是在应用程序打开后不久,它会因以下 logcat 而崩溃

$ adb shell am start -n "com.ashb.osrstimers/com.ashb.osrstimers.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Connected to process 10303 on device 'huawei-mar_lx1a-JRQDU20512003719'.
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
D/ActivityThread: Attach thread to application
I/ashb.osrstimer: QarthPatchMonintor::Init
    QarthPatchMonintor::StartWatch
    QarthPatchMonintor::WatchPackage: /data/hotpatch/fwkhotpatch/
    QarthPatchMonintor::CheckAndWatchPatch: /data/hotpatch/fwkhotpatch/com.ashb.osrstimers
    QarthPatchMonintor::CheckAndWatchPatch: /data/hotpatch/fwkhotpatch/all
    QarthPatchMonintor::Run
I/ashb.osrstimer: QarthPatchMonintor::Reading
I/ashb.osrstimer: QarthPatchMonintor::CheckNotifyEvent
    QarthPatchMonintor::CheckNotifyEvent before read
I/HwApiCacheMangerEx: apicache path=/storage/emulated/0 state=mounted key=com.ashb.osrstimers#10247#256
I/HwApiCacheMangerEx: apicache path=/storage/emulated/0 state=mounted key=com.ashb.osrstimers#10247#0
I/AwareBitmapCacher: init processName:com.ashb.osrstimers pid=10303 uid=10247
E/AwareLog: AtomicFileUtils: readFileLines file not exist: android.util.AtomicFile@5e2870f
E/AwareLog: AtomicFileUtils: readFileLines file not exist: android.util.AtomicFile@b34439c
D/AwareBitmapCacher: handleInit switch not opened pid=10303

该错误似乎显示在这些行上:

E/AwareLog: AtomicFileUtils: readFileLines file not exist: android.util.AtomicFile@5e2870f
E/AwareLog: AtomicFileUtils: readFileLines file not exist: android.util.AtomicFile@b34439c

这是我从 json 文件中读取数据的代码:

private String getJSONData() {
        String json_string;

        AssetManager assetManager = getAssets();
        InputStream input;

        try {
            input = assetManager.open("timers.json");
            int size = input.available();
            byte[] buffer = new byte[size];
            input.read(buffer);
            input.close();
            json_string = new String(buffer);

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return json_string;
    }

标签: javaandroidjsonfile

解决方案


推荐阅读