1

I am doing an android app which would save the password for the next time the user wants to use this app. when I try to run my application, the password is entered,but there is a pop-up said the application has stopped?

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;
    }   

    {

}

}   
4

3 回答 3

0

使用 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);
于 2013-10-18T05:59:12.290 回答
0

我不确定,但我认为你在这一点上错过了一些东西:

return getSharedPreferences(null, 0);

看这里:

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

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

如果您将“名称”设置为空,他将无法创建文件。

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

private final String KEY_SHAREDPREFS = "tmpsharedprefs";

然后使用它

return getSharedPreferences(KEY_SHAREDPREFS, 0);
于 2013-10-18T05:43:28.110 回答
0

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

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

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

于 2013-10-18T05:52:16.573 回答