【问题标题】:Add sharedPreferences to listpreference values将 sharedPreferences 添加到 listpreference 值
【发布时间】:2013-07-16 00:23:43
【问题描述】:

我的应用程序从启动画面开始,然后转到 MainActivity 取决于用户偏好,该偏好由选择 listpreference 中的任何选项决定:

1- 启动应用程序,没有启动画面和音乐。

2- 仅启动应用程序。

3- 启动带有启动画面和音乐的应用程序。

我实现了 listpreference 但我无法将 sharedPreferences 添加到每个值,因此在检查其中任何一个值后,它存储在 sharedPreferences 中并启动应用程序取决于用户偏好,

因为这是第一次使用 listpreference,而且我是 android 新手,我不知道该怎么做,

任何帮助将不胜感激。

Splash.java

 public class Splash extends Activity{  
MediaPlayer ourSong;
@Override
protected void onCreate(Bundle savedInstanceState) {
    this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
         setContentView(R.layout.splash);  

    ourSong = MediaPlayer.create(Splash.this, R.raw.splashsound);     
    ourSong.start();

    Thread timer = new Thread(){
        public void run(){
            try{
                sleep(2000); 
                }
              catch (InterruptedException e){
                e.printStackTrace(); 
                }
              finally{
                  Intent intent = new Intent(Splash.this, MainActivity.class);                                     
                  startActivity(intent); 
                  }
            }                                   
        };
         timer.start();   
         }  
@Override
protected void onPause() {
            // TODO Auto-generated method stub
    super.onPause();
    ourSong.release();
    finish();
          } 
       }

Prefs.java

public class Prefs extends PreferenceActivity{

@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    addPreferencesFromResource(R.xml.prefs); 
    }
}

prefs.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
  xmlns:android="http://schemas.android.com/apk/res/android">

     <ListPreference
       android:title="Application Start Preference"
       android:summary="This preference allows to select how the app will start"
       android:key="listPref"
       android:entries="@array/splash"
       android:entryValues="@array/splash_values" />          
</PreferenceScreen>

arrays.xml

<?xml version="1.0" encoding="utf-8"?>
 <resources>   
  <string-array name="splash">
      <item>start app without splash screen </item>
      <item>start app with splash scrren only</item>
      <item>start app with splash screen and music</item>
  </string-array>

  <string-array name="splash_values">
      <item>1</item>
      <item>2</item>
      <item>3</item>

  </string-array>    
</resources>

更新: 我尝试了下面的代码,但发生的情况是应用程序正常运行以启动然后转到 mainactivity 然后我在选项菜单中按首选项它确定它打开首选项页面然后按 listpreference 它会弹出 3 单选按钮的对话框,如果我按任何一个单选按钮没问题,然后我来回返回直到退出应用程序,然后再次打开它直接打开以显示 mainactivity 没有飞溅然后立即崩溃。

logcat 中的第 23 行也是:

 int splashType = getPrefs.getInt("listPref", 0);

Splash.java

public class Splash extends Activity{  
 MediaPlayer ourSong;
 @Override
  protected void onCreate(Bundle savedInstanceState) {
   this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
   // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
      setContentView(R.layout.splash);  

     SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences
             (getBaseContext());

 int splashType = getPrefs.getInt("listPref", 0);
  switch (splashType) {
  case 2:
Intent intent = new Intent(Splash.this, MainActivity.class);                                     
       startActivity(intent);
  break;

  case 1:
   setContentView(R.layout.splash);  
    Thread timer = new Thread()
   {
    public void run()
    {
        try
        {
            sleep(2000); 
        }
        catch (InterruptedException e)
        {
            e.printStackTrace(); 
        }
        finally
        {
            Intent intent = new Intent(Splash.this, MainActivity.class);                                     
            startActivity(intent);  
         }
      }                          
    };
   timer.start();       
break;

 case 0:
   ourSong = MediaPlayer.create(Splash.this, R.raw.splashsound); 
   ourSong.start();

Thread timer1 = new Thread(){
    public void run(){
        try{
            sleep(2000); }
          catch (InterruptedException e){
            e.printStackTrace(); }
          finally{
          Intent intent = new Intent(Splash.this, MainActivity.class);                                     
                startActivity(intent); 
                    }
                   }                                    
                 };
           timer1.start();   
  break;
   }
  }
@Override
protected void onPause() {
     // TODO Auto-generated method stub
    super.onPause();
    ourSong.release();
    finish();
      } 
   }

LOGCAT:

 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tsn.dr/com.tsn.dr.Splash}: java.lang.ClassCastException: java.lang.String
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.ClassCastException: java.lang.String
at android.app.ContextImpl$SharedPreferencesImpl.getInt(ContextImpl.java:2968)
at com.tsn.dr.Splash.onCreate(Splash.java:23)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)

【问题讨论】:

  • Splash 中的第 23 行是什么。另外,我不清楚究竟会发生什么。你点击偏好,然后它崩溃了?
  • @codeMagic 对不起:发生了什么是应用程序正常运行以启动启动然后转到 mainactivity 然后我在选项菜单中按首选项它可以打开首选项页面然后按 listpreference 它会弹出 3 单选按钮的对话框如果我按下任何单选按钮就可以了然后我返回直到退出应用程序然后再次打开它直接打开到 mainactivity 没有飞溅然后在这里崩溃第 23 行是:int splashType = getPrefs.getInt("splashPref", 0);

标签: android sharedpreferences listpreference


【解决方案1】:

您可以尝试类似的方法来获取存储的用户偏好,然后采取相关措施:

SharedPreferences sharedPrefs;
sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
String userSplashValue = sharedPrefs.getString("listPref", "1");

if (userSplashValue.equals ("1")) {
 // choice 1
}
else if (userSplashValue.equals("2")) {
 // choice 2    
}
else if (userSplashValue.equals ("3")){
 // choice 3    
}

【讨论】:

  • 它不起作用,您在列表首选项中按任何选项然后返回或退出应用程序,然后直接显示启动屏幕并在其上堆叠,谢谢
  • 所以你不再抛出 RunTimeException 了?然后您能否发布您的新代码,同时确保您使用的是正确的密钥 - 在您的情况下,它是“listPref”而不是“splashPref”
  • 我稍微调整了你的代码,但最后它可以工作了,非常感谢。
猜你喜欢
  • 1970-01-01
  • 2020-11-17
  • 1970-01-01
  • 2018-09-22
  • 2015-02-01
  • 1970-01-01
  • 2014-07-06
  • 1970-01-01
  • 2021-10-28
相关资源
最近更新 更多