BufferedReader 在 Android 应用程序中返回 null,但在 Java 应用程序中不返回 null
我已经用 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;
It是 br.readLine() 由于某种原因返回 null 。我查了一下,bufferedreader 似乎完全支持。
编辑:好的,我发现它实际上并没有找到该文件,但可能试图执行 isCorrect() 方法,因为它是静态的。我现在已经删除了 method 和 br 声明静态标记,并将配置文件文本文件移动到包含所有 java 文件的文件夹中,但仍然找不到它。我应该如何参考该职位?如果我只是通过名称引用它,Android 查找该文件的默认位置是什么?非常感谢!
I've written a application in Java which I am trying to port to Android now, but for some reason when using the readLine() method from the BufferedReader it is returning null. I have tested the code in Java and it works fine no problem. The file I am reading from is a plain text file which I have put in with the rest of my java files. My code simple takes in user input for a login and upon clicking the login button checks the details by reading in the text file with the profiles in. Below is a simplication of my code:
OnClickListener in main activity:
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...");
}
});
}
The LoginModel.java class:
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;
It is the br.readLine() which is returning null for some reason. I've looked it up and bufferedreader seems fully supported.
Edit: Ok, I've found out that it's not actually finding the file but was probably trying to execute the isCorrect() method because it was static. I've removed the method and br declaration static tags now, and moved the profiles text file to the folder with all my java files in but it still can't find it. How should I reference the position? What is the default position for android to look for the file if I simply reference it by name? Many thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定这是否是 NPE 的实际原因,但它绝对是一个错误:
您正在构造函数中初始化
br
,但在isCorrect()
中使用它这是静态的!它不需要调用 oreder 中的LoginModel
封闭类,因此在使用br
之前可能根本不会调用 c'tor。您应该在声明时或在静态范围内初始化
br
,以便在类加载时对其进行初始化。编辑:请注意,它还表明
br
被声明为静态 - 因此LogicModel
的所有实例实际上都使用br< 的相同对象/代码>!
,但如果没有更多代码,就不可能确定它。br.readLine()
返回 null 的原因 - 可能是您已经在对isCorrect()
的其他调用中耗尽了该文件,或者使用了 < code>brI am not sure if it is the actual cause of the NPE but it is definetly a bug:
You are initializing
br
in your constructor, but you use it inisCorrect()
which is static! It does not require an enclosing class ofLoginModel
in oreder to be invoked, so the c'tor might not be called at all before usingbr
.You should initialize
br
when declaring it, or in a static scope, so it will be initialized when the class loads.EDIT: Note that it also indicates that
br
is declared static - so all instances ofLogicModel
actually use the same object ofbr
!The reason why
br.readLine()
is returning null - might be you already exhausted the file in other calls toisCorrect()
, or other methods which usebr
, but it will be impossible to know it for certain without more code.您确定 FileNotFoundException 没有被捕获吗?这意味着 Android 无法找到该文件,因此无法创建 BufferedReader,从而将其保留为空。在 logcat 中搜索进程的 FileNotFoundException(通常为橙色)或使用断点进行调试。
编辑:
在 android 中使用文件时,通常将它们放在 res/raw 文件夹中。这是因为 Android 没有文件的主要起点。事实上,android 更喜欢限制对文件的访问(因为逻辑安全问题)。
所以你必须做的:
将文件放在 res/raw 文件夹中并将其重命名为“profiles”
(Android不允许资源中使用大写)(也许你必须
首先创建文件夹)。
编辑您的构造函数以接收
resources (将其更改为:“public LoginModel(Resources res)”)
相应地编辑初始化(更改“LoginModel login = new
LoginModel();" 到 "LoginModel login = new LoginModel(getResources());")
然后将 "br = new BufferedReader(new FileReader(file));" 更改为 "br =
新的BufferedReader(新
InputStreamReader(res.openRawResources(R.raw.profiles)));"
删除行 "file = new File("Profiles");"
Are you sure the FileNotFoundException wasn't caught? This would mean that Android couldn't find the file and therefore couldn't create the BufferedReader, thereby leaving it null. Search in logcat for a FileNotFoundException (usually in orange) by your process or debug using a breakpoint.
Edit:
When using files in android one usually places them in the res/raw folder. This is because android doesn't have a main starting point for files. In fact android prefers to restrict access to files (because of logical safety issues).
So what you'd have to do:
Place the file in the res/raw folder and rename it to "profiles"
(Android doesn't allow uppercases in resources)(maybe you have to
create the folder first).
Edit your constructor to receive the
resources (change it to: "public LoginModel(Resources res)")
Edit the initializing accordingly (changing "LoginModel login = new
LoginModel();" to "LoginModel login = new LoginModel(getResources());")
Then change "br = new BufferedReader(new FileReader(file));" to "br =
new BufferedReader(new
InputStreamReader(res.openRawResources(R.raw.profiles)));"
Remove the line "file = new File("Profiles");"