无法在构造函数中访问 SharedPreferences
我使用 SharedPreferences 来存储数据。当我尝试从类的构造函数调用数据存储函数时,它显示运行时错误。但是,当我从 onCreate() 调用该函数时,它工作正常。
有什么方法可以从构造函数调用我的数据存储函数吗?
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.setOnClickListener(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);
}
}
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.
Is there any way I can call my data-storing function from the constructor?
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.setOnClickListener(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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您试图在初始化视图或变量之前访问文本视图:
当您从 Activity 构造函数调用
LoadPreferences
时,尚未调用onCreate
,这表示textSavedMem1
和textSavedMem2
尚未初始化且为null
。当您从
onCreate
调用它时,只要在以下内容之后,这些变量就会被初始化,一切都会按预期工作。You are attempting to access your text views before you have initialised the view or the variables:
When you call
LoadPreferences
from the Activity constructor,onCreate
hasn't yet been called, which meanstextSavedMem1
andtextSavedMem2
have not been initialised and arenull
.When you call it from
onCreate
, so long as it is after the following, then these variable are initialised and everything works as expected.这是正常的。当您的活动创建时,例如调用构造函数时,SharePreferences 对象尚无法访问。使用/覆盖活动的构造函数不是一个好习惯。阅读Activity 生命周期了解如何使用 Activity。
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.
这是不可能的,因为你可以在已经创建的活动中获取 SharedPreference 。在这里,你甚至在活动正在构建(构造函数)之前就尝试获取 SharedPreference 。为了获取共享首选项,你需要
Activity
..您可以将值存储在构造函数内的某些实例变量中,然后将它们放入 SharedPreference 中...
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 needactivity
..U can store the values in some Instance variables inside constructor then put them in SharedPreference...