我是C#和Android開發(使用Xamarin)的新手。我試圖創建簡單的登錄屏幕來檢查用戶輸入,如果用戶輸入與文本文件中的數據相匹配,它將顯示登錄成功,否則它會顯示登錄失敗。將EditText與C#中的字符串比較
我不得不提到,我爲此掙扎了近一天。我認爲我已經搞亂了編碼,所以我創建了簡單的控制檯應用程序,在那裏複製了代碼,並且它完美地工作,所以我認爲我的問題與將字符串與來自EditText的用戶輸入進行比較有關。
應用程序構建成功,但每次按下與下面顯示的方法相關的按鈕時,我的應用程序崩潰並將我拋出。
我創建了兩個名爲user.txt和pass.txt的文本文件。 pass.txt中的第一行是用戶在user.txt的第一行的密碼。
再一次,我不得不提及代碼在控制檯應用程序中完美工作(我用簡單的Console.Readline和帶有Console.Writeline的Toast.MakeText替換了EditText中的user_input)。請問我做錯了什麼?
public void read_list(object sender, EventArgs e)
{
k = FindViewById<EditText>(Resource.Id.user);
z = FindViewById<EditText>(Resource.Id.pass);
string user_input = k.Text.ToString();
string pass_input = z.Text.ToString();
int count = 0;
bool valid = false;
const string person = "user.txt";
List<string> users = new List<string>();
using (StreamReader username = new StreamReader(person))
{
string line;
while ((line = username.ReadLine()) != null)
{
users.Add(line);
}
foreach (string s in users)
{
if (Equals(s, user_input))
{
valid = true;
break;
}
count++;
}
}
const string key = "pass.txt";
List<string> passwords = new List<string>();
bool pass_valid = false;
using (StreamReader password = new StreamReader(key))
{
string pass_line;
while ((pass_line = password.ReadLine()) != null)
{
passwords.Add(pass_line);
}
if (Equals(passwords[count], pass_input))
{
pass_valid = true;
}
else
{
pass_valid = false;
}
}
if ((pass_valid) && (valid))
{
Toast.MakeText(this, "Login successful", ToastLength.Short).Show();
}
else
{
Toast.MakeText(this, "Login failed", ToastLength.Short).Show();
}
}
如果應用程序崩潰,請顯示錯誤日誌 –
@языкK:沒有錯誤日誌。應用程序只是崩潰,並說「不幸的是,應用程序停止工作」,並拋出我。在Visual Studio或其他東西中沒有一個錯誤。 – matejcro
我非常懷疑。你可以檢查輸出頁面(在VS2013中查看>輸出),看看是否有任何例外?應用程序不會在沒有提供任何問題的情況下崩潰......除非你抑制了所有錯誤。 –