繁体   English   中英

BufferedReader在Android应用程序中返回null,但在Java应用程序中不返回null

[英]BufferedReader returning null in Android application but not in Java application

我已经用Java编写了一个应用程序,现在尝试将其移植到Android,但是由于某些原因,当使用BufferedReader中的readLine()方法时,它返回null。 我已经用Java测试了代码,并且工作正常,没有问题。 我正在读取的文件是纯文本文件,已与其他Java文件一起放入。 我的代码很简单,需要用户输入登录信息,单击登录按钮后,通过读取带有配置文件的文本文件来检查详细信息。以下是我的代码的简化:

主要活动中的OnClickListener:

button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            EditText textUsername = (EditText) findViewById(R.id.textUsername);
            EditText textPassword = (EditText) findViewById(R.id.textPassword);

            // Collect the login details entered by the user
            String username = textUsername.getText().toString();
            String password = textPassword.getText().toString();

            // Check if login details are correct
            LoginModel login = new LoginModel();
            Correct = login.isCorrect(username, password); // LINE COMMENTED
            // OUT SO THAT DETAILS DON'T NEED TO BE ENTERED

            //              Correct = true; // used to bypass login for now
            if (Correct) { // if details are correct then start main program
                Intent intent = new Intent (LoginView.this, MenuListView.class);
                startActivity(intent);
            }
            else System.out.println("Login is incorrect...");

        }
    });
}

LoginModel.java类:

public LoginModel() {
    file = new File("Profiles");
    try {
        br = new BufferedReader(new FileReader(file));
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    System.out.println("Login Constructor successful!");
}

// Checks whether the inputted details are correct
public static boolean isCorrect(String u, String p) {
    System.out.println(p);
    boolean check = false;
    String line = null;
    try {
        do {
            line = br.readLine();   // This line is returning null
            // System.out.println("Checking profile : " + line);
            String[] info = line.split("\t");
            // nested if-statement to improve efficiency over &&
            if (info[0].equals(u)) {
                System.out.println("username found!");
                if (info[1].equals(p)) {
                    System.out.println("password correct!");
                    check = true;
                } else
                    System.out.println("password incorrect!");
            } else
                System.out.println("username not found!");
        } while (line != null && check == false);
    return check;

出于某种原因,它是br.readLine()返回null。 我查了一下,似乎完全支持bufferedreader。

编辑:好的,我发现它实际上并没有找到文件,但是可能试图执行isCorrect()方法,因为它是静态的。 我现在已经删除了方法和br声明静态标签,并将配置文件文本文件移动到了包含所有Java文件的文件夹中,但仍然找不到。 我应该如何参考职位? 如果我仅按名称引用,Android查找文件的默认位置是什么? 非常感谢!

我不确定这是否是NPE的真正原因,但绝对是一个错误:

您正在构造函数中初始化br ,但是您可以在isCorrect()使用它,这是静态的! 它不需要在oreder中调用LoginModel的封闭类,因此在使用br之前根本可以不调用c'tor。

您应在声明br或在静态作用域中初始化br ,以便在类加载时将其初始化。

编辑:请注意,这也表明br被声明为静态的-因此LogicModel所有实例实际上都使用br的相同对象!
br.readLine()返回null的原因-可能是您在其他调用isCorrect()或使用br其他方法中已经用尽了文件,但是如果没有更多代码,就无法确定它。

您确定没有捕获FileNotFoundException吗? 这将意味着Android无法找到文件,因此无法创建BufferedReader,从而将其保留为空。 在进程中的logcat中搜索FileNotFoundException(通常为橙色),或使用断点进行调试。

编辑:在android中使用文件时,通常将它们放在res / raw文件夹中。 这是因为android没有文件的主要起点。 实际上,android更喜欢限制对文件的访问(由于逻辑安全问题)。

所以你要做的是:

  1. 将文件放在res / raw文件夹中,并将其重命名为“ profiles”(Android不允许资源中使用大写字母)(也许您必须先创建该文件夹)。

  2. 编辑您的构造函数以接收资源(将其更改为:“ public LoginModel(Resources res)”)

  3. 相应地编辑初始化(将“ LoginModel login = new LoginModel();”更改为“ LoginModel login = new LoginModel(getResources());”)

  4. 然后更改“ br = new BufferedReader(new FileReader(file));” 到“ br = new BufferedReader(new InputStreamReader(res.openRawResources(R.raw.profiles)))”;“

  5. 删除行“ file = new File(“ Profiles”);“

暂无
暂无

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

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