简体   繁体   English

java退出应用程序导致无限循环

[英]java exit app causes infinite loop

i have made a simple java app to print a text file then should terminate.我制作了一个简单的 java 应用程序来打印一个文本文件,然后应该终止。 I tried to use the return statemend but didn't work at all.我尝试使用 return 语句,但根本不起作用。 So i used the system.exit(0) but instead of exit the app, it goes in an infinite loop... this is my code: can someone tell me what is wrong ?所以我使用了 system.exit(0) 但不是退出应用程序,而是进入无限循环......这是我的代码:有人可以告诉我出了什么问题吗?

package com.example.testprinterdemo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;

import com.pda3505.Service.CaptureService;
import com.pda3505.printer.PrinterClassSerialPort;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;


public class MainActivity extends Activity {

    public static PrinterClassSerialPort printerClass = null;

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

        init();

        printerClass.write(new byte[] { 0x1d, 0x21,0x00,0x00});
        printerClass.write(new byte[] { 0x1b, 0x26, 0x00 });


        BufferedReader reader = null;
        try {reader = new BufferedReader(new FileReader("/mnt/sdcard/temp.txt"));} 
        catch (FileNotFoundException e) {e.printStackTrace();}
        String line = null;
        try {line = reader.readLine();} 
        catch (IOException e) {e.printStackTrace();}
        while(line!=null) {
            printerClass.printText(line);
            printerClass.printText("\n");
            try {line = reader.readLine();} 
            catch (IOException e) {e.printStackTrace();}
        }
        system.exit(0);
    }


    private void init() {       

        printerClass = new PrinterClassSerialPort(new Handler());
        printerClass.open(this);

        Intent newIntent = new Intent(MainActivity.this, CaptureService.class);
        newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startService(newIntent);

    }

}
    try{
        //never hard code the file path in Android.
        String filePath = Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator+"temp.txt";
        BufferedReader reader = new BufferedReader(new FileReader(filePath));
        while (true){
            String line = reader.readLine();
            if(line.length() > 0){
                printerClass.printText(line);
                printerClass.printText("\n");
            }else{
                break;
                //you can use return too
            }
        }
    }catch (IOException e){
        e.printStackTrace();
    }

In your approach, line is never null, it is either "" or it is " " or "\\n" etc, but never null.在您的方法中, line 永远不会为空,它要么是""要么是" ""\\n"等,但永远不会为空。 so listen for length and break the loop所以听长度并打破循环

Please avoid multiple try catch blocks when you can have your code under single try block.当您可以将代码置于单个 try 块下时,请避免使用多个 try catch 块。 And please scope the variables properly, do not declare variables outside the scope where it is not used, as it causes problems for JAVA Garbage collector/并且请正确定义变量的范围,不要在不使用的范围之外声明变量,因为它会导致 JAVA 垃圾收集器/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM