无法在构造函数中访问 SharedPreferences

发布于 2024-11-19 08:09:25 字数 2545 浏览 4 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

厌味 2024-11-26 08:09:25

您试图在初始化视图或变量之前访问文本视图:

textSavedMem1.setText(strSavedMem1);
textSavedMem2.setText(strSavedMem2);

当您从 Activity 构造函数调用 LoadPreferences 时,尚未调用 onCreate,这表示 textSavedMem1textSavedMem2 尚未初始化且为 null

当您从 onCreate 调用它时,只要在以下内容之后,这些变量就会被初始化,一切都会按预期工作。

setContentView(R.layout.main); 

textSavedMem1 = (TextView)findViewById(R.id.savedmem1);
textSavedMem2 = (TextView)findViewById(R.id.savedmem2);

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);
不爱素颜 2024-11-26 08:09:25

这是正常的。当您的活动创建时,例如调用构造函数时,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.

朦胧时间 2024-11-26 08:09:25

这是不可能的,因为你可以在已经创建的活动中获取 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 need activity..

U can store the values in some Instance variables inside constructor then put them in SharedPreference...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文