我的应用程序从启动画面开始,然后转到 MainActivity 取决于用户偏好,该偏好由在 listpreference 中选择任何选项决定:
1-启动没有飞溅和音乐的应用程序。
2-仅启动应用程序。
3-启动带有飞溅和音乐的应用程序。
我实现了 listpreference 但我无法将 sharedPreferences 添加到每个值,因此在检查其中任何一个值后,它存储在 sharedPreferences 中并启动应用程序取决于用户偏好,
因为这是第一次使用 listpreference 并且我是 android 新手,所以我不知道该怎么做,
任何帮助将不胜感激。
飞溅.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();
}
}
首选项.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);
}
}
首选项.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>
数组.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);
飞溅.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();
}
}
日志猫:
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)