2016-05-06 17 views
0

共有設定を使用してログインアクティビティを作成しました。コードは以下のとおりです。ログインアクティビティ共有優先を使用する

ログインするとエラーが発生します。常にパスワードを表示するのは間違っています。

私のプロジェクトを完成させるために、どんなボディもコードを改善するのに役立ちます。

UserSessionクラス

package com.achal089.pestcontrol; 

import android.content.Context; 
import android.content.Intent; 
import android.content.SharedPreferences; 

import java.util.HashMap; 

/** 
* Created by as on 5/7/2016. 
*/ 
public class UserSession { 

    SharedPreferences pref; 

    // Editor reference for Shared preferences 
    SharedPreferences.Editor editor; 

    // Context 
    Context _context; 

    // Shared preferences mode 
    int PRIVATE_MODE = 0; 

    // Shared preferences file name 
    public static final String PREFER_NAME = "Register"; 

    // All Shared Preferences Keys 
    public static final String IS_USER_LOGIN = "IsUserLoggedIn"; 

    // User name (make variable public to access from outside) 
    public static final String Email = "Email"; 

    // Email address (make variable public to access from outside) 
    public static final String Password = "Password"; 


    // Constructor 
    public UserSession(Context context){ 
     this._context = context; 
     pref = _context.getSharedPreferences(PREFER_NAME, PRIVATE_MODE); 
     editor = pref.edit(); 
    } 

    //Create login session 
    public void createUserLoginSession(String uName, String uPassword){ 
     // Storing login value as TRUE 
     editor.putBoolean(IS_USER_LOGIN, true); 

     // Storing name in preferences 
     editor.putString(Email, uName); 

     // Storing email in preferences 
     editor.putString(Password, uPassword); 

     // commit changes 
     editor.commit(); 
    } 

    /** 
    * Check login method will check user login status 
    * If false it will redirect user to login page 
    * Else do anything 
    * */ 
    public boolean checkLogin(){ 
     // Check login status 
     if(!this.isUserLoggedIn()){ 

      // user is not logged in redirect him to Login Activity 
      Intent i = new Intent(_context, Login.class); 

      // Closing all the Activities from stack 
      i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

      // Add new Flag to start new Activity 
      i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

      // Staring Login Activity 
      _context.startActivity(i); 

      return true; 
     } 
     return false; 
    } 



    /** 
    * Get stored session data 
    * */ 
    public HashMap<String, String> getUserDetails(){ 

     //Use hashmap to store user credentials 
     HashMap<String, String> user = new HashMap<String, String>(); 

     // user name 
     user.put(Email, pref.getString(Email, null)); 

     // user email id 
     user.put(Password, pref.getString(Password, null)); 

     // return user 
     return user; 
    } 

    /** 
    * Clear session details 
    * */ 
    public void logoutUser(){ 

     // Clearing all user data from Shared Preferences 
     editor.clear(); 
     editor.commit(); 

     // After logout redirect user to MainActivity 
     Intent i = new Intent(_context, MainActivity.class); 

     // Closing all the Activities 
     i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

     // Add new Flag to start new Activity 
     i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

     // Staring Login Activity 
     _context.startActivity(i); 
    } 


    // Check for login 
    public boolean isUserLoggedIn(){ 
     return pref.getBoolean(IS_USER_LOGIN, false); 
    } 
} 

ログイン活動

package com.achal089.pestcontrol; 

import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 
import android.widget.Toast; 

public class Login extends Activity { 

    private static final String PREFER_NAME = "Register"; 

    UserSession session; 

    private SharedPreferences sharedPreferences; 



    TextView login; 
    TextView pass; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_login); 



    login = (TextView)findViewById(R.id.email); 
     pass = (TextView)findViewById(R.id.password); 


     Button buttonLogin = (Button) findViewById(R.id.btnLogin); 

     sharedPreferences = getSharedPreferences(PREFER_NAME, Context.MODE_PRIVATE); 


     // Login button click event 
     buttonLogin.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 

       // Get username, password from EditText 
       String username = login.getText().toString(); 
       String password = pass.getText().toString(); 

       // Validate if username, password is filled    
       if(username.trim().length() > 0 && password.trim().length() > 0){ 
        String uName = null; 
        String uPassword =null; 

        if (sharedPreferences.contains("Email")) 
        { 
         uName = sharedPreferences.getString("Email", ""); 

        } 

        if (sharedPreferences.contains("Password")) 
        { 
         uPassword = sharedPreferences.getString("Password", ""); 

        } 

        // Object uName = null; 
        // Object uEmail = null; 
        if(username.equals(uName) && password.equals(uPassword)){ 

         session.createUserLoginSession(uName, 
           uPassword); 

         // Starting MainActivity 
         Intent i = new Intent(getApplicationContext(),MainActivity.class); 
         i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

         // Add new Flag to start new Activity 
         i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
         startActivity(i); 

         finish(); 

        }else{ 

         // username/password doesn't match& 
         Toast.makeText(getApplicationContext(), 
           "Username/Password is incorrect", 
           Toast.LENGTH_LONG).show(); 

        } 
       }else{ 

        // user didn't entered username or password 
        Toast.makeText(getApplicationContext(), 
          "Please enter username and password", 
          Toast.LENGTH_LONG).show(); 

       } 

      } 
     }); 




    } 



    public void Reg(View view) 
    { 
     Intent a = new Intent(Login.this,Register.class); 
     startActivity(a); 
    } 


} 
} 

登録活動

package com.achal089.pestcontrol; 

import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.TextView; 

public class Register extends Activity { 

    UserSession session; 
    SharedPreferences sharedpreferences; 
    TextView name; 
    TextView address; 
    TextView email; 
    TextView phone; 
    TextView occupation; 
    TextView password; 
    private static final String PREFER_NAME = "Register"; 

    public static final String Name = "nameKey"; 
    public static final String Email = "emailKey"; 
    public static final String Phone = "Phone"; 
    public static final String Address = "Address"; 
    public static final String Occupation = "Occupation"; 
    public static final String Password = "Password"; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_register); 

     name = (TextView) findViewById(R.id.editText6); 
     address = (TextView) findViewById(R.id.editText7); 
     email = (TextView) findViewById(R.id.editText9); 
     phone = (TextView) findViewById(R.id.editText8); 
     password = (TextView)findViewById(R.id.editText11); 
     occupation = (TextView)findViewById(R.id.editText10); 
     sharedpreferences = getSharedPreferences(PREFER_NAME, 
       Context.MODE_PRIVATE); 
     if (sharedpreferences.contains(Name)) { 
      name.setText(sharedpreferences.getString(Name, "")); 
     } 
     if (sharedpreferences.contains(Email)) { 
      email.setText(sharedpreferences.getString(Email, "")); 

     } 

     if (sharedpreferences.contains(Occupation)) { 
      occupation.setText(sharedpreferences.getString(Occupation, "")); 
     } 

     if (sharedpreferences.contains(Password)) { 
      password.setText(sharedpreferences.getString(Password, "")); 
     } 


     if (sharedpreferences.contains(Address)) { 
      address.setText(sharedpreferences.getString(Address, "")); 

     } 

     if (sharedpreferences.contains(Phone)) { 
      phone.setText(sharedpreferences.getString(Phone, "")); 

     } 



    } 


    public void Save(View view) { 
     String n = name.getText().toString(); 
     String e = email.getText().toString(); 
     String w = phone.getText().toString(); 
     String m = address.getText().toString(); 
     String p = password.getText().toString(); 
     String v = occupation.getText().toString(); 
     SharedPreferences.Editor editor = sharedpreferences.edit(); 
     editor.putString(Name, n); 
     editor.putString(Email, e); 
     editor.putString(Phone, w); 
     editor.putString(Address,m); 
     editor.putString(Occupation,v); 
     editor.putString(Password,p); 
     editor.commit(); 


     Intent a = new Intent(Register.this, Login.class); 
     startActivity(a); 
    } 

    public void Login (View view) 
    { 
     Intent a = new Intent(Register.this, Login.class); 
     startActivity(a); 
    } 
} 
+0

LoginActivityはUserSessionと同じコードです。更新してください。 – AgileNinja

+0

私はそれを更新してください –

+0

まあ、どの文字列がSharedPreferencesにありますか?既に文字列が入っていません。 –

答えて

0

あなたは適切UserSessionクラスを使用していないようです。

あなたはこの

sharedpreferences = getSharedPreferences(mypreference, 
      Context.MODE_PRIVATE); 

を行い、(文字列パラメータが一致しない)session変数とログイン活動とは異なるSharedPreferencesを使用しました。プラス、セッション変数は、レジスタのアクティビティコードで完全に未使用と表示され、あなたは両方のクラスののonCreate内部

session = new UserSession(getApplicationContext()); 

のようにそれを使用することができ、ログイン活動に正しく

を使用していません。どちらのクラスでもSharedPreferencesオブジェクトは必要ありません。

SharedPreferencesの定義に使用した文字列と一致する方法を使用することも、実際にUserSessionコードを使用することもできます。その決定はあなた次第ですが、私は2つを混ぜないことをお勧めします。

the tutorialのコードが完全な使い方を参照する場所に戻ってもいいかもしれません。あなたのログイン活動で

0

、あなたはあなたがあなたの登録クラスにユーザー名とパスワードを初期化してきたと思った「登録」共有好みを、取得しました。しかし、あなたの登録クラス:あなたのログインクラスで

public static final String mypreference = "mypref";//this name is different 

、あなたが使用している:あなたからのunameとパスワードを取得するときは、必ずデフォルト値を取得しますなぜ

private static final String PREFER_NAME = "Register"; 

ですログインアクティビティに共有設定を登録すると、検証によって常にfalseが返されます。 クラスに登録するには、共有設定の名前を「登録」に変更します。

+0

同じエラーが発生しても同じエラーが返されます –

+0

私はコードを更新してください –

関連する問題