簡體   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