创建文本文件使我的应用程序崩溃

时间:2018-08-27 10:24:29

标签: java android android-camera barcode

我正在制作此应用程序,它是一个条形码扫描仪,用于扫描VIN;它可以识别VIN,但我想将该数字保存在txt文件或XML中。

代码是:

@Override
    public void scannedCode(String code) throws IOException {
        if (singleScan) {
            finishScan(code);
        }
        vincodeView.setText(code);
        vincodeView.setVisibility(View.VISIBLE); 
//Makes the scanned VIN visible on the display. If deleted the app crashes. 
// ---------------------- Code that should create the txt file, but it make the app crash as soon as it detects the barcode ----------------------------
Context context = null;
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(context.openFileOutput("trial.txt", Context.MODE_PRIVATE));
outputStreamWriter.write(code);
outputStreamWriter.close();
    }

我对Java和移动开发完全陌生,因为实习错误,我才来到这里。我不知道是什么问题。我试图找到解决办法,但一无所获。我尝试使用其他版本的代码将字符串保存到txt文件中,但是什么也没有。像创建保存类一样,我尝试了这一点,但是看起来它无法访问变量“代码”。

整个代码基于https://github.com/bees4honey/mobile_vin_scanner。 我以他们的SDK为基础。

您能看到一个解决方案吗,甚至只是一些建议也是有用的。

谢谢

-----更新----- Logcat:

Process: com.bees4honey.vinscanner, PID: 22864
java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.FileOutputStream android.content.Context.openFileOutput(java.lang.String, int)' on a null object reference
    at com.bees4honey.vinscanner.ScannerActivity.scannedCode(ScannerActivity.java:133)
    at com.bees4honey.vinscanner.ScannerFragment$ScannerHandler.handleMessage(ScannerFragment.java:346)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:157)
    at android.app.ActivityThread.main(ActivityThread.java:5571)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:745)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:635)

0 个答案:

没有答案