私はAndroidに移植しようとしているJavaでアプリケーションを作成しましたが、何らかの理由でBufferedReaderのreadLine() 。私はJavaでコードをテストし、問題なく動作します。私が読んでいるファイルは、私が残りのjavaファイルに入れたプレーンテキストファイルです。私のコードの簡単なログイン用とでプロファイルを持つテキストファイルを読み込むことにより、ログインボタンをチェック詳細をクリックすると、ユーザー入力を取り込み、以下の私のコードのsimplicationです。主な活動でAndroidアプリケーションではnullを返すが、Javaアプリケーションではnullを返すBufferedReader
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;
これは、いくつかの理由でnullを返しているbr.readLine()です。私はそれを見て、bufferedreaderは完全にサポートされているようだ。
編集:私はそれが実際にファイルを見つけるのではなく、おそらくisCorrect()メソッドを静的だったので実行しようとしていたことがわかりました。メソッドとbr宣言静的タグを削除し、プロファイルのテキストファイルをすべてのJavaファイルが入ったフォルダに移動しましたが、まだ見つかりません。どのように私はその位置を参照する必要がありますか?私が単に名前で参照すると、アンドロイドがファイルを探すデフォルトの位置は何ですか?どうもありがとう!
これは良い点です。私はちょうどJavaを始めました。スコープの問題を避けるために静的なメソッドをたくさん宣言していましたが、明らかにたくさんあります今私が気づいている落とし穴のうちのひとつ!私は静的修飾子を削除しましたが、エラーを取り除いていません。テキストファイルから読んでいる場合は、Androidプロジェクトにどこに配置すればよいですか? – user1058210
何らかの理由で実際のファイル名が必要な場合は、アセットを使用します。これは別のフォルダ[project]/assetsにあります。それ以外の場合は、[project]/res/rawフォルダがあるrawリソースを使用できます。 Resources Managerを使用してResources.openRawResource(http://developer.android.com/reference/android/content/res/Resources.html#openRawResource(int))または(アセットを使用する場合は)http: //developer.android.com/reference/android/content/res/AssetManager.html)。 – Boude
@ user1058210:brがnullを返した理由については、私の編集をお読みください。 – amit