简体   繁体   English

保存具有共享首选项的切换

[英]save toggle with shared preferences

I want allow the user can enable or disable the splash screen. 我希望允许用户启用或禁用启动屏幕。

i have a toggle button and have two classes: the first class, named SplashActivity , handles the splash screen, defined in this class: 我有一个切换按钮,有两个类:第一类,名为SplashActivity ,处理在该类中定义的初始屏幕:

package com.test.splash;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;


public class SplashActivity extends Activity {
/** Called when the activity is first created. */

public SharedPreferences preferences;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    SharedPreferences sharedPref = getSharedPreferences("sharedPref1", MODE_PRIVATE);


    boolean sharedPref1 = preferences.getBoolean("sharedPref1", true);
    //???.setChecked(sharedPrefs.getBoolean("NameOfThing  ToSave", true));
    if (sharedPref1 = true) //if (tgpref) may be enough, not sure
    {

MediaPlayer mp = MediaPlayer.create(this, R.raw.play);

mp.start();
    Handler handler = new Handler();
    handler.postDelayed(new Runnable(){
        public void run(){
            finish();
            startActivity(new Intent (SplashActivity.this,asli.class));
        }
    }, 1000);
}
    else
    {
        startActivity(new Intent (SplashActivity.this,asli.class));
    }
    }
}

The second class is named asli and at this class I defined a shared preferences to save toggle state. 第二个类名为asli ,在这个类中,我定义了一个共享首选项来保存切换状态。

package com.test.splash;


import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ToggleButton;



public class asli extends PreferenceActivity{
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.asli);


    final ToggleButton toggle = (ToggleButton) findViewById(R.id.toggleButton1);
    toggle.setOnClickListener(new OnClickListener(){
    public void onClick(View v) 
    {
        if (toggle.isChecked()) 
        {
            SharedPreferences.Editor editor = 
getSharedPreferences("sharedPref1", MODE_PRIVATE).edit();
            editor.putBoolean("sharedPref1", true);
            editor.commit();
        }
        else
        {

SharedPreferences.Editor editor =
getSharedPreferences("sharedPref2", MODE_PRIVATE).edit();
            editor.putBoolean("sharedPref2", false);
            editor.commit();
        }
    }

});

}
}

The Problem is you are not initialized preferences . 问题是您尚未初始化preferences

you defined preferences in your class but you didn't initialize it right . 您在课堂上定义了preferences ,但是您没有正确地初始化它。

public SharedPreferences preferences;

you are getting a boolean from null variable in this line : 您将在此行中从null变量获取布尔值:

boolean sharedPref1 = preferences.getBoolean("sharedPref1", true);

so before using preferences you should initialize it like : 所以在使用preferences之前,您应该像这样初始化它:

preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM