2016-09-12 7 views
0

共有の環境設定を使用してセッションを維持しているという点で、私はアプリケーションを開発しています。ユーザーログイン時に、さらにメールを使用しています。別のアクティビティではnullを示しています。 mainActivityのための私のコードで以下 :共有設定を使用してセッションを維持する方法

public class main extends AppCompatActivity { 
public Button submit; 


public static final String MyPREFERENCES = "MyPrefs" ; 
public static final String email = "emailkey"; 
SharedPreferences sharedpreferences; 

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

    submit = (Button) findViewById(R.id.btn_login); 

    ImageView i1 = (ImageView) findViewById(R.id.imgLogo); 

    String checkBoxText = "I agree to all the"; 
    final CheckBox checkBox = (CheckBox) findViewById(R.id.checkBox); 
    sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE); 
    checkBox.setText(Html.fromHtml(checkBoxText)); 
    checkBox.setMovementMethod(LinkMovementMethod.getInstance()); 
    submit.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      EditText e1 = (EditText) findViewById(R.id.input_email); 
      EditText p1 = (EditText) findViewById(R.id.input_password); 
      String e = e1.getText().toString(); 
      final String password = p1.getText().toString(); 

      SharedPreferences.Editor editor = sharedpreferences.edit(); 

      editor.putString(email, e); 

      editor.commit(); 

ここでは、他の活動のために私のコードです:

  String e,email; 

@Override 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.internshipsdetails); 
     SharedPreferences sharedpreferences = getSharedPreferences(main.MyPREFERENCES, Context.MODE_PRIVATE); 

    sharedpreferences.getString(email,e); 
    String url = "url?COMP_REQ_ID=" + title + "&StuEmail=" + e; 
    AQuery mAQuery = new AQuery(InternShipsDetails.this); 
    mAQuery.ajax(url, String.class, new AjaxCallback<String>() { 

     @Override 

     public void callback(String url, String data, AjaxStatus status) { 

      super.callback(url, data, status); 
      if (BuildConfig.DEBUG) { 
       Log.d("###$Request URL", url + ""); 
       Log.d("###$Response ", data + ""); 
       Log.d("###$Status Message : ", status.getMessage() + ""); 
       Log.d("###$Status Code : ", status.getCode() + ""); 

      } 

答えて

3

あなたはので、あなたがnullになっていることを共有県に間違ったキーを渡しています。続き

はあなたに保存されたメールアドレス与えるあなたの更新されたコードです:コーディングハッピー

 SharedPreferences sharedpreferences = getSharedPreferences(main.MyPREFERENCES, Context.MODE_PRIVATE); 

    e = sharedpreferences.getString(main.email,""); 
    String url = "url?COMP_REQ_ID=" + title + "&StuEmail=" + e; 

を!

+0

だろう – user6798766

6

ログアウト操作を実行した後で共有設定値を変更してセッションを管理し、ログイン時に再度設定を確認することもできます。現在の時刻をログインとログアウト時の文字列に保存することもできます。プライベート共有設定を使用するようにしてください。

0

getString (String key, String defValue)メソッドは、Stringを返します。ですから、string変数で出力をキャッチする必要があります。 また、nullというキーを渡しています。値を保存したものと同じキーを渡す必要があります。

だから、あなたのケースでは、秀、それが働いているmuch..now thanku

e = sharedpreferences.getString("emailkey","nodata"); 
関連する問題