Cannot access SharedPreferences in constructor
I use SharedPreferences
for storing data. When I try to call the data-storing function from the constructor of my class, it shows a Runtime error. However, when I call the function from onCreate() it works fine.
package com.examples.storedata;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Activity1 extends Activity
{
EditText editText1, editText2;
TextView textSavedMem1, textSavedMem2;
Button buttonSaveMem1, buttonSaveMem2;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textSavedMem1 = (TextView)findViewById(R.id.savedmem1);
textSavedMem2 = (TextView)findViewById(R.id.savedmem2);
editText1 = (EditText)findViewById(R.id.edittext1);
editText2 = (EditText)findViewById(R.id.edittext2);
buttonSaveMem1 = (Button)findViewById(R.id.save_mem1);
buttonSaveMem2 = (Button)findViewById(R.id.save_mem2);
buttonSaveMem1.setOn开发者_StackOverflow社区ClickListener(buttonSaveMem1OnClickListener);
buttonSaveMem2.setOnClickListener(buttonSaveMem2OnClickListener);
}
public Activity1()
{
LoadPreferences(); //show error while calling from here
}
Button.OnClickListener buttonSaveMem1OnClickListener
= new Button.OnClickListener(){
public void onClick(View arg0) {
SavePreferences("MEM1", editText1.getText().toString());
LoadPreferences();
}
};
Button.OnClickListener buttonSaveMem2OnClickListener
= new Button.OnClickListener(){
public void onClick(View arg0) {
SavePreferences("MEM2", editText2.getText().toString());
LoadPreferences();
}
};
private void SavePreferences(String key, String value)
{
SharedPreferences sharedPreferences = getPreferences(MODE_APPEND);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(key, value);
editor.putInt("1",5);
editor.commit();
}
private void LoadPreferences()
{
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
String strSavedMem1 = sharedPreferences.getString("MEM1", "");
String strSavedMem2 = sharedPreferences.getString("MEM2", "");
textSavedMem1.setText(strSavedMem1);
textSavedMem2.setText(strSavedMem2);
}
}
You are attempting to access your text views before you have initialised the view or the variables:
textSavedMem1.setText(strSavedMem1);
textSavedMem2.setText(strSavedMem2);
When you call LoadPreferences
from the Activity constructor, onCreate
hasn't yet been called, which means textSavedMem1
and textSavedMem2
have not been initialised and are null
.
When you call it from onCreate
, so long as it is after the following, then these variable are initialised and everything works as expected.
setContentView(R.layout.main);
textSavedMem1 = (TextView)findViewById(R.id.savedmem1);
textSavedMem2 = (TextView)findViewById(R.id.savedmem2);
This is normal. By the time your activity is created e.g when the constructor is called SharePreferences object is not yet accessible. It's not good practice to use/override activity's constructor. Read about activity lifecycle to get the idea how to use activity.
Its not possible since u can get SharedPreference
in activity which has already been created.. here u r trying to get SharedPreference even before activity is under construction (Constructor).. for getting shared preference u need activity
..
U can store the values in some Instance variables inside constructor then put them in SharedPreference...
精彩评论