【问题标题】:How can I set save and load a password with sharedPreference?如何使用 sharedPreference 设置保存和加载密码?
【发布时间】:2013-10-26 20:48:15
【问题描述】:

我正在做一个 android 应用程序,它会在用户下次想要使用这个应用程序时保存密码。当我尝试运行我的应用程序时,输入了密码,但有一个弹出窗口说应用程序已停止?

package com.wheresmyphone;

import android.os.Bundle;
import android.app.Activity;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;


   public class Check extends Activity {


    String StringPreference;

    SharedPreferences preferences;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_check);
    Button b = (Button)findViewById(R.id.Button01);
    final EditText preferences = (EditText)findViewById(R.id.txt12345);

    b.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            EditText sharedPreferences = (EditText)findViewById(R.id.txt12345);
            String StringPreference = preferences.getText().toString();

        }



    });
}
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.check, menu);
        return true;
    }
    /**
     *   Method used to get Shared Preferences */

    public SharedPreferences getPreferences() 
    {

        return getSharedPreferences(null, 0);
    }
    /**
     *  Method used to save Preferences */
    public void savePreferences(String key, String value)
    {
        SharedPreferences sharedPreferences = getPreferences();
        SharedPreferences.Editor editor = preferences.edit();
        String StringPreference =  sharedPreferences.toString();
        editor.putString("sharedString", StringPreference);
        editor.commit();
    }

/**
     *  Method used to load Preferences */
    public String loadPreferences(String key) 
    {
        try {
            SharedPreferences sharedPreferences = getPreferences();
            String strSavedMemo = sharedPreferences.getString(key, "");
            return strSavedMemo;
        } catch (NullPointerException nullPointerException) 
        {
            Log.e("Error caused at  TelaSketchUtin loadPreferences method",
                    ">======>" + nullPointerException);
            return null;
        }
    }
    /**
     *  Method used to delete Preferences */
    public boolean deletePreferences(String key)
    {
        SharedPreferences.Editor editor=getPreferences().edit();
        editor.remove(key).commit();
        return false;
    }   

    {

}

}   

【问题讨论】:

  • preferences 在这种情况下为空
  • 我相信你需要学习如何命名变量。您正在为 SharedPreferences preferences;final EditText preferences = (EditText)findViewById(R.id.txt12345); 使用变量名称 preferences。尝试为不同类型的变量保留不同的名称。

标签: android passwords sharedpreferences


【解决方案1】:

我不确定,但我认为您此时错过了一些东西:

return getSharedPreferences(null, 0);

看这里:

http://developer.android.com/reference/android/content/Context.html#getSharedPreferences%28java.lang.String,%20int%29

参数 名称
所需的首选项文件。如果此名称的首选项文件不存在,它将在您检索编辑器 (SharedPreferences.edit()) 并提交更改 (Editor.commit()) 时创建。

如果你把“name”设置为null,他就不能创建文件了。

可能是解决方案,将其添加到顶部

private final String KEY_SHAREDPREFS = "tmpsharedprefs";

然后使用它

return getSharedPreferences(KEY_SHAREDPREFS, 0);

【讨论】:

    【解决方案2】:

    看到您正在尝试从首选项中获取字符串,首先您必须像此代码一样从编辑文本中获取密码

    EditText passwordText = (EditText)findViewById(R.id.txt12345);
    String password = passwordText.getText().toString();
    

    然后将此密码存储在共享首选项中

    【讨论】:

      【解决方案3】:

      使用 SharedPreferences 作为,

      保存:

      SharedPreferences settings;
      SharedPreferences.Editor editor;
      public static final String PREFS_NAME = "app_pref";
      public static final String KEY_p_id = "KEY_test";
      
      settings = getSharedPreferences(PREFS_NAME, 0);
      editor = settings.edit();
      editor.putString(Login_screen.KEY_test, values.get(0));
      editor.commit();
      

      删除:

      editor.remove("KEY_test").commit();
      

      获取:

      settings = getSharedPreferences(PREFS_NAME, 0);
      String TestId = settings.getString("KEY_test", null);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-06-29
        • 1970-01-01
        • 2021-11-30
        • 1970-01-01
        • 1970-01-01
        • 2017-11-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多