Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/191.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
还记得我的功能在android中不起作用吗_Android_Login - Fatal编程技术网

还记得我的功能在android中不起作用吗

还记得我的功能在android中不起作用吗,android,login,Android,Login,我是Android开发的新手。在我的代码中,我使用共享首选项来存储和检索用户名和密码。我通过我学习的一个教程实现了这一点。我创建了一个方法,即记住()当我在click方法中单击复选框时调用它。但是当我单击复选框并运行我的程序时,不幸的是,它被停止了 这是我的密码。有人能找到我哪里出错了吗 public void onClick(View v) { if(chkbx.isChecked()) remember(); fina

我是Android开发的新手。在我的代码中,我使用共享首选项来存储和检索用户名和密码。我通过我学习的一个教程实现了这一点。我创建了一个方法,即记住()当我在click方法中单击复选框时调用它。但是当我单击复选框并运行我的程序时,不幸的是,它被停止了

这是我的密码。有人能找到我哪里出错了吗

public void onClick(View v) {
            if(chkbx.isChecked())
            remember();

            final TextView username =(TextView)findViewById(R.id.username);
            final TextView password =(TextView)findViewById(R.id.password);
            String uname = username.getText().toString();
            String pass =  password.getText().toString();


               String storedPassword=loginDataBaseAdapter.getSinlgeEntry(uname);
            if(!uname.equals("")  && !pass.equals("")&&pass.equals(storedPassword))
                startActivity(new Intent(LoginActivity.this,welcomeActivity.class).putExtra("usr",(CharSequence)uname));
             else 

                Toast.makeText(LoginActivity.this,"Invalid UserName or Password", Toast.LENGTH_LONG).show();



        }
    });
这是记住的方法

private void remember(){

        final TextView username =(TextView)findViewById(R.id.username);
        final TextView password =(TextView)findViewById(R.id.password);
        String uname = username.getText().toString();
        String pass =  password.getText().toString();
        SharedPrefManager.SetName(uname); 
            SharedPrefManager.SetName(pass); 
         SharedPrefManager.StoreToPref();

        SharedPrefManager.LoadFromPref(); 
         String usrname,pswd;
        usrname = SharedPrefManager.GetName();
       pswd= SharedPrefManager.GetPass();


       EditText tv = null;
        tv = (EditText)findViewById(R.id.username);
        tv.setText(usrname);
        tv = (EditText)findViewById(R.id.password);
        tv.setText(pswd);

    }

使用下面的代码来记住我的功能,它对我有用。 首先创建一个如下所示的类

package com.example.fitnessapp;

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

public class SessionManager {

    SharedPreferences pref;
    Editor editor;
    Context context;

    int PRIVATE_MODE = 0;

    public static final String PREF_NAME = "FitPref";
    private static final String IS_LOGIN = "IsLoggedIn";
    public static final String KEY_USERNAME = "username";
    public static final String KEY_PASSWORD = "password";
    private boolean isChecked = false;

    public SessionManager(Context context) {
        this.context = context;
        pref = context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
        editor = pref.edit();
    }

    public void createLoginSession(String username, String password) {
        editor.putBoolean(IS_LOGIN, true);

        editor.putString(KEY_USERNAME, username);
        editor.putString(KEY_PASSWORD, password);
        if (isChecked == true) {
            editor.putBoolean("isChecked", true);
        } else {
            editor.putBoolean("isChecked", false);
        }
        editor.commit();
    }

    public void createLoginSession(String username, String password, boolean remember) {
        editor.putBoolean(IS_LOGIN, true);

        editor.putString(KEY_USERNAME, username);
        editor.putString(KEY_PASSWORD, password);
        editor.putBoolean("remember", remember);
        editor.commit();
    }

    public void checkLogin() {
        if (!this.isLoggedIn()) {
            Intent i = new Intent(context, MainActivity.class);

            i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

            context.startActivity(i);
        }
    }

    public boolean isLoggedIn() {
        return pref.getBoolean(IS_LOGIN, false);
    }
}
之后,在MainActivity中使用以下代码作为存储用户名和密码

if (remember.isChecked() == true) {
    sm.createLoginSession(user, pass, true);
   } 
要检索回该用户名和密码,请使用以下代码

SharedPreferences pref = MainActivity.this.getSharedPreferences(SessionManager.PREF_NAME, 0);
        boolean loggedIn = pref.getBoolean("remember", false);
        if (loggedIn == true) {
            remember.setChecked(true);
            String user = pref.getString(SessionManager.KEY_USERNAME, "");
            String pass = pref.getString(SessionManager.KEY_PASSWORD, "");

            username.setText(user);
            password.setText(pass);
        }

希望它能帮助您。

可以
SharedPrefManager.SetName(pass)SharedPrefManager.SetPass(String)
?我有单独的文件sharedManager,其中我定义了SetPass和SetPass方法。我不知道哪里出错了?在检索密码之后,您正在使用
SetName
方法存储它(您正在使用该方法存储用户名)。使用
SharedPrefManager.SetPass(pass)
而不是
SharedPrefManager.SetName(pass)记忆()
methodya我更改了它,但什么也没发生lucianThanks Jitesh。我有一个疑问。变量sm是SessionManager类的对象,对吗?当我在代码中实现它时,它要求我将sm变量初始化为NULL。这是否正确?是的,但如果指定sm=NULL,请不要忘记检查NULL条件