【问题标题】:Using SharedPreferences for saving Application's Setting使用 SharedPreferences 保存应用程序的设置
【发布时间】:2014-12-19 13:48:16
【问题描述】:

可以说,我的应用有 2 个活动。

  1. Activity 1,主 Activity 带有一个打开 Activity 2 的按钮。
  2. Activity 2,这是我的设置 Activity,有一个checkBox

现在我正在使用 SharedPreferences 保存值,无论复选框是否被选中。

现在我想在所有其他活动中使用此设置值,所以我在活动 1 中制作了一个 toast,它会显示复选框是否被选中。

活动 1 中的代码:-

if(new Activity2().getCheckStatus())
    Toast.makeText(getApplicationContext(), "chcked", Toast.LENGTH_SHORT).show();
else
    Toast.makeText(getApplicationContext(), "not chcked", Toast.LENGTH_SHORT).show();

活动 2:-

public class Activity2 extends Activity implements OnClickListener
{
    CheckBox check =null;

    SharedPreferences settings;

    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        getActionBar().setTitle("SETTING");

        setContentView(R.layout.setting);

         check =(CheckBox)findViewById(R.id.checkBox);
         check.setOnClickListener(this);

         settings = Activty2.this.getSharedPreferences("checkBox",0);

        Boolean a = settings.getBoolean("isChecked", false);
            check.setChecked(a);
    }

    /* (non-Javadoc)
     * @see android.view.View.OnClickListener#onClick(android.view.View)
     */
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch(v.getId())
        {

        case R.id.checkBox:


            SharedPreferences.Editor editor = settings.edit();

            if(!check.isChecked())
            {
                check.setChecked(true);
                editor.putBoolean("isChecked", true);
                editor.commit();
            }
            else
            {
                check.setChecked(false);
                editor.putBoolean("isChecked", false);
                editor.commit();
            }
            break;
        }
    }

    public boolean getCheckStatus() 
    {
        SharedPreferences mysettings;
        try{
             mysettings = Activity2.this.getSharedPreferences("checkBox",0);
        }catch(NullPointerException e)
        {
            return false;
        }

        return mysettings.getBoolean("isChecked", false);


    }
}

Activity2 仅用于显示是否选中了 checkBox,如果选中则显示,如果有任何更改,则使用 SharedPreferences 保存

我捕获 NullPointerException 的原因是,因为我在 activity1 中显示 toast,这是我的主要活动,所以每当应用是第一次运行,在我运行活动 2 的onCreate() 之前不会有 SharedPref。因此,如果我收到异常,则表示该应用程序是第一次运行,或者用户直到现在还没有打开 Activity2。如果是,则返回默认的 false 值,如果不是,则永远不会调用 catch(),并且我会存储该值。

我的问题:- 如您所料,它不起作用。复选框工作正常,每当我打开 Activity2 时,它都会显示之前的设置值,但吐司总是显示 not checkd

请告诉我,这里的错误是什么,或者是否有任何其他更简单的方法可以保存 android 应用程序用户设置并在应用程序的其他任何地方访问它们。

谢谢。

【问题讨论】:

  • 你不应该使用 new 操作符构造一个新的 Activity。曾经。它应该始终使用 Intent 创建。

标签: android sharedpreferences


【解决方案1】:

首先,将读取的 SharedPrefs 移到 activity1 中,像这样

if(new Activity2().getCheckStatus())

如果 Activity 2 更大,将是致命的。

所以这个:

public boolean getCheckStatus() 
{
    SharedPreferences mysettings;
    try{
         mysettings = getSharedPreferences("checkBox",0);
    }catch(NullPointerException e)
    {
        return false;
    }

    return mysettings.getBoolean("isChecked", false);
}

转到活动 1。Albo,将 "checkBox" 设为常量值(公共静态最终字符串)。

立即尝试。

【讨论】:

    【解决方案2】:

    您应该在其他活动中执行相同操作(打开共享首选项)并检查您的复选框是否已设置:

    settings = Activity.this.getSharedPreferences("checkBox",0);
    Boolean a = settings.getBoolean("isChecked", false);
    

    【讨论】:

      猜你喜欢
      • 2010-12-21
      • 2012-11-26
      • 2011-07-19
      • 1970-01-01
      • 1970-01-01
      • 2017-03-14
      • 1970-01-01
      • 2015-10-28
      • 2016-03-20
      相关资源
      最近更新 更多