我正在设计自己的Android应用程序,我希望可以自定义启动画面。我希望用户能够打开和关闭启动画面(它可以工作!)并设置启动画面的持续时间(这里有轻微问题:D)。这是我的启动画面活动:
package info.androidhive.slidingmenu;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
public class SplashScreen2 extends Activity {
SharedPreferences pref=PreferenceManager.getDefaultSharedPreferences(getBaseContext());
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
boolean x = pref.getBoolean("checkbox", true);
String time = pref.getString("list", "4000");
// Splash screen timer
int SPLASH_TIME_OUT = Integer.parseInt(time);
if(x) {
setContentView(R.layout.activity_splash2);
new Handler().postDelayed(new Runnable() {
/*
* Showing splash screen with a timer. This will be useful when you
* want to show case your app logo / company
*/
@Override
public void run() {
// This method will be executed once the timer is over
// Start your app main activity
Intent i = new Intent(SplashScreen2.this, MainActivity.class);
startActivity(i);
// close this activity
finish();
}
}, SPLASH_TIME_OUT);
}
else {
Intent x1 = new Intent(SplashScreen2.this, MainActivity.class);
startActivity(x1);
}
}
protected void onPause() {
super.onPause();
finish();
}
}
首选项xml文件:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
android:title="Splash Screen" >
<EditTextPreference
android:key="name"
android:title="EditText"
android:summary="Enter your name"/>
<CheckBoxPreference
android:summary="Check to enable splash screen"
android:title="Enable"
android:key="checkbox"/>
<ListPreference
android:entries="@array/list"
android:summary="Choose duration of splash screen"
android:key="list"
android:entryValues="@array/value"
android:title="Duration"/>
</PreferenceScreen>
数组文件:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="list">
<item>1 second</item>
<item>2 second</item>
<item>3 second</item>
<item>4 second</item>
</string-array>
<string-array name="value">
<item>1000</item>
<item>2000</item>
<item>3000</item>
<item>4000</item>
</string-array>
</resources>
我的logcat:
12-18 17:12:47.139: E/AndroidRuntime(8302): FATAL EXCEPTION: main
12-18 17:12:47.139: E/AndroidRuntime(8302): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{info.androidhive.slidingmenu/info.androidhive.slidingmenu.SplashScreen2}: java.lang.NullPointerException
12-18 17:12:47.139: E/AndroidRuntime(8302): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1925)
12-18 17:12:47.139: E/AndroidRuntime(8302): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2028)
12-18 17:12:47.139: E/AndroidRuntime(8302): at android.app.ActivityThread.access$600(ActivityThread.java:127)
12-18 17:12:47.139: E/AndroidRuntime(8302): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1179)
12-18 17:12:47.139: E/AndroidRuntime(8302): at android.os.Handler.dispatchMessage(Handler.java:99)
12-18 17:12:47.139: E/AndroidRuntime(8302): at android.os.Looper.loop(Looper.java:137)
12-18 17:12:47.139: E/AndroidRuntime(8302): at android.app.ActivityThread.main(ActivityThread.java:4508)
12-18 17:12:47.139: E/AndroidRuntime(8302): at java.lang.reflect.Method.invokeNative(Native Method)
12-18 17:12:47.139: E/AndroidRuntime(8302): at java.lang.reflect.Method.invoke(Method.java:511)
12-18 17:12:47.139: E/AndroidRuntime(8302): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
12-18 17:12:47.139: E/AndroidRuntime(8302): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
12-18 17:12:47.139: E/AndroidRuntime(8302): at dalvik.system.NativeStart.main(Native Method)
12-18 17:12:47.139: E/AndroidRuntime(8302): Caused by: java.lang.NullPointerException
12-18 17:12:47.139: E/AndroidRuntime(8302): at android.content.ContextWrapper.getPackageName(ContextWrapper.java:127)
12-18 17:12:47.139: E/AndroidRuntime(8302): at android.preference.PreferenceManager.getDefaultSharedPreferencesName(PreferenceManager.java:371)
12-18 17:12:47.139: E/AndroidRuntime(8302): at android.preference.PreferenceManager.getDefaultSharedPreferences(PreferenceManager.java:366)
12-18 17:12:47.139: E/AndroidRuntime(8302): at java.lang.Class.newInstanceImpl(Native Method)
12-18 17:12:47.139: E/AndroidRuntime(8302): at java.lang.Class.newInstance(Class.java:1319)
12-18 17:12:47.139: E/AndroidRuntime(8302): at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
12-18 17:12:47.139: E/AndroidRuntime(8302): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1916)
12-18 17:12:47.139: E/AndroidRuntime(8302): ... 11 more
但是,只要应用程序启动,它就会在启动画面出现之前崩溃。这个应用程序在添加持续时间选项之前有效。帮助!
答案 0 :(得分:0)
将此代码放在 onCreate()中,而不是在其他任何地方之前。
SharedPreferences pref=PreferenceManager.getDefaultSharedPreferences(getBaseContext());
答案 1 :(得分:0)
您可以尝试自己的偏好吗?
SharedPreferences mPreference = context.getSharedPreferences("your_app", Context.MODE_PRIVATE);
//Save user time
mPreference.edit().putLong("DURATION", 3000).apply();
//Get duration
long duration = mPreference.getLong("DURATION", 3000);
答案 2 :(得分:0)
java.lang.NullPointerException
之外的SharedPreferences pref=PreferenceManager.getDefaultSharedPreferences(getBaseContext());
引起的{p> onCreate()
是根本原因。
所以将代码更改为
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences pref=PreferenceManager.getDefaultSharedPreferences(getBaseContext());
boolean x = pref.getBoolean("checkbox", true);
String time = pref.getString("list", "4000");
// Splash screen timer
int SPLASH_TIME_OUT = Integer.parseInt(time);
if(x) {
setContentView(R.layout.activity_splash2);
new Handler().postDelayed(new Runnable() {
/*
* Showing splash screen with a timer. This will be useful when you
* want to show case your app logo / company
*/
@Override
public void run() {
// This method will be executed once the timer is over
// Start your app main activity
Intent i = new Intent(SplashScreen2.this, MainActivity.class);
startActivity(i);
// close this activity
finish();
}
}, SPLASH_TIME_OUT);
}
else {
Intent x1 = new Intent(SplashScreen2.this, MainActivity.class);
startActivity(x1);
}
}
答案 3 :(得分:0)
检查此代码:
public class SplashScreen2 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences pref=PreferenceManager.getDefaultSharedPreferences(SplashScreen2.this);
boolean x = pref.getBoolean("checkbox", true);
String time = pref.getString("list", "4000");
// Splash screen timer
...