2015-11-06 30 views
0

我是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(); 
     } 


    } 
+0

如果應用程序崩潰,請顯示錯誤日誌 –

+0

@языкK:沒有錯誤日誌。應用程序只是崩潰,並說「不幸的是,應用程序停止工作」,並拋出我。在Visual Studio或其他東西中沒有一個錯誤。 – matejcro

+0

我非常懷疑。你可以檢查輸出頁面(在VS2013中查看>輸出),看看是否有任何例外?應用程序不會在沒有提供任何問題的情況下崩潰......除非你抑制了所有錯誤。 –

回答

0

所以,正如我所說,你的主要概念是錯誤的。讓我們看看我的例子中,檢查了這一點:
MainActivity看起來像

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

using Android.App; 
using Android.Content; 
using Android.OS; 
using Android.Runtime; 
using Android.Views; 
using Android.Widget; 
using System.IO; 

namespace TestStuff 
{ 
    [Activity(Label = "Main",MainLauncher=true,Icon = "@drawable/icon")]    
    public class MainActivity : Activity 
    { 
     //path of folder,where we want to save our .txt File(Downloads) 
     Java.IO.File FolderToSave = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads); 

     protected override void OnCreate(Bundle bundle) 
     { 
      base.OnCreate(bundle); 

      SetContentView(Resource.Layout.Second); 

      EditText login = FindViewById<EditText>(Resource.Id.editText1); 
      EditText password = FindViewById<EditText>(Resource.Id.editText2); 
      Button btn = FindViewById<Button>(Resource.Id.btn); 
      //here is final Path of our Credentials.txt 
      string filePath = System.IO.Path.Combine(FolderToSave.Path, "Credentials.txt"); 
      //method,that will write correct login/password to .txt file 
      this.WriteFile("Vasya", "12345",filePath); 
      //eventhandler,when we pressing the button,method "check credentials will do some stuff 
      btn.Click += (sender, e) => 
       { 
        //put parametrs: location of Credentials.txt and current login and password 
    CheckForCredentials(filePath,login.Text,password.Text); 
        }; 

     } 
     //create and write credentials.txt with login and pass 
     void WriteFile(string Login,string Password,string Path) 
     { 
      using (StreamWriter Credentials = new StreamWriter (Path)) 
      { 
       //first row will be our login 
       Credentials.WriteLine(Login); 
       //second is password 
       Credentials.WriteLine(Password); 
      } 
     } 

     //check for correct data 
     void CheckForCredentials(string Path,string CurrentLogin,string CurrentPassword) 
     { 
      string Login, Password; 
      if (File.Exists(Path)) 
      { 
       //reading from Credentials.txt first rows(that is our login) 
       Login = File.ReadLines(Path).First(); 
       //same for password 
       Password = File.ReadLines(Path).Last(); 

       //check if introduced values are correct! 
       if (Login == CurrentLogin && Password == CurrentPassword) 
       { 
        Toast.MakeText(this, "Credentials are correct", ToastLength.Short).Show(); 
       } 
       else 
       { 
        Toast.MakeText(this, "Something going wrong", ToastLength.Short).Show(); 
       } 
      } 
      else 
      { 
       //credentials for some reason doest exist in your app. 
       Toast.MakeText(this, "Not exist file", ToastLength.Short).Show(); 
      } 
     } 
    } 
} 

MainLayout.axml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 
     <EditText 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/editText1" 
      android:layout_centerInParent="true" 
      android:layout_marginLeft="60dp" 
      android:hint="UserName" /> 
     <EditText 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_below="@id/editText1" 
      android:id="@+id/editText2" 
      android:layout_centerInParent="true" 
      android:layout_marginLeft="60dp" 
      android:hint="Password" /> 
     <Button 
      android:text="Check Credentials" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@id/editText2" 
      android:id="@+id/btn" 
      android:layout_centerInParent="true" 
      android:layout_marginTop="20dp" /> 
    </RelativeLayout> 
</LinearLayout> 

此外,你需要把權限在AndroidManifest.xml中(位於文件夾Propreties)this:

  1. WriteExternalStorage
  2. ReadExternalStorage

,這是所有:)。