Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.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 将多个字符串传递给SharedReference_Android_String_Sharedpreferences - Fatal编程技术网

Android 将多个字符串传递给SharedReference

Android 将多个字符串传递给SharedReference,android,string,sharedpreferences,Android,String,Sharedpreferences,我想存储三个字符串作为我的应用程序的用户首选项。我已经设置了一个很好的布局,只需将字符串保存到SharedReference即可。我还想知道如何在下一个活动中检索这些字符串。下面是我当前的代码,如果有人能告诉我如何将此功能添加到代码中,我将不胜感激。这是我的应用程序中的一个障碍,我已经尝试了几天了 主要活动代码: package com.amritayalur.mypowerschool; import android.app.Activity; import android.conten

我想存储三个字符串作为我的应用程序的用户首选项。我已经设置了一个很好的布局,只需将字符串保存到SharedReference即可。我还想知道如何在下一个活动中检索这些字符串。下面是我当前的代码,如果有人能告诉我如何将此功能添加到代码中,我将不胜感激。这是我的应用程序中的一个障碍,我已经尝试了几天了

主要活动代码:

package com.amritayalur.mypowerschool;



import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;



public class MyPowerSchoolActivity extends Activity {
Button buttonSubmit;
TextView textViewTitle;
TextView textViewDesc;
EditText editTextURL, editTextUser, editTextPass;


 String str;
 String username;
 String password;
 String url;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    buttonSubmit = (Button) findViewById(R.id.buttonSubmit);
    textViewTitle = (TextView) findViewById(R.id.textViewTitle);
    textViewDesc = (TextView) findViewById(R.id.textViewDesc);

    editTextURL = (EditText) findViewById(R.id.editTextURL);
    editTextUser = (EditText) findViewById(R.id.editTextUser);
    editTextPass = (EditText) findViewById(R.id.editTextPass);
    //Start TextView
    textViewTitle.setText("MyPowerSchool");


    //button listener
    buttonSubmit.setOnClickListener(new View.OnClickListener() {

         @Override
            public void onClick(View v) 
            {
                if (  ( !editTextURL.getText().toString().equals("")) && (
!editTextUser.getText().toString().equals("")) && (
!editTextPass.getText().toString().equals("") ) ) 
                {

                    url = editTextURL.getText().toString();
                    username = editTextUser.getText().toString();
                    password = editTextPass.getText().toString();


                    // TODO Auto-generated method stub
                    //Intent i = new Intent( MyPowerSchoolActivity.this,
creds.class);    


                   //startActivity(i);



                }
            };




});}}

只需在要使用SharedReferences的活动中设置这些类变量:

public static String MY_PREFS = "MY_PREFS";
private SharedPreferences mySharedPreferences;
int prefMode = Activity.MODE_PRIVATE;
然后存储字符串值:

SharedPreferences.Editor editor = mySharedPreferences.edit();
editor.putString("key1", "value1");
editor.putString("key2", "value2");
editor.putString("key3", "value3");

editor.commit(); // persist the values
要在其他活动/课程中阅读它们:

mySharedPreferences = getSharedPreferences(MY_PREFS, prefMode);
String string1 = mySharedPreferences.getString("key1", null);
String string2 = mySharedPreferences.getString("key2", null);

这是不难查找和找到的例子。通过转到,您将找到有关如何在Android手机上使用数据存储的链接。

以某种方式定义您的首选字段名称。通常的做法是通过最终的静态场来实现。它可以在你的活动课上,在一些单独的课上完成。给他们一些uniq的名字

public static final String MY_PARAM_1 = "com.amritayalur.mypowerschool.PARAM_1";
public static final String MY_PARAM_2 = "com.amritayalur.mypowerschool.PARAM_2";
获取共享首选项实例

// "this" is your activity or context
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); 
要将字符串放入preferenes,需要创建一个编辑器

SharedPreferences.Editor editor = prefs.edit();
然后把你的价值观

editor.putString(MY_PARAM_1, "First string");
editor.putString(MY_PARAM_2, "Second string");
完成后,提交更改

editor.commit();
要恢复值,请使用getString方法

// specify default value in case if preferences are empty or current key is not set
prefs.getString(MY_PARAM_1, "default value");

可以像这样添加多个值:

preferences.edit()
      .putLong(DAYS_LEFT, premiumStatus.getDaysLeft())
      .putString(KEY, premiumStatus.getKey())
      .putString(ID, premiumStatus.getId())
      .apply();

第一个设置共享首选项。为此,请在“创建”之前设置此行:

  private Context mContext; 
然后将其放入oncreate中:

                    mContext = this;   
然后将您的值存储在SharedPrefere上:

  private Context mContext; 
 SharedPreferences settings = 
PreferenceManager.getDefaultSharedPreferences(mcontext); 
SharedPreferences.Editor editor = settings.edit();

url = editTextURL.getText().toString();
username = editTextUser.getText().toString();
password = editTextPass.getText().toString();

editor.putString("kye1", url);
editor.putString("kye2", username);
editor.putString("kye3", password);
然后从另一个类(或任何位置)检索您的值:


然后在任意位置设置此“值1/2/3”

我正在接近一个力量。我可以把它放在OnClick方法中吗?您可以在Logcat中读取堆栈跟踪,以更好地了解是什么导致强制关闭(异常)。如果是NullPointerException,则尝试将“null”替换为getString(“key1”,null)中的第二个参数,如下所示:getString(“key1”,null)。不用说,在读取这些值之前,您需要先放置并提交这些值。是的,您可以在方法内部执行putString/commit和getString。