我正在设计我自己的android应用程序this后,我希望启动画面是可定制的。我希望用户能够设置启动画面的打开和关闭(它的工作!),并设置启动画面的持续时间(这里有一个小问题: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
但是每当应用程序启动时,它都会在启动屏幕出现之前崩溃。在我添加持续时间选项之前,这个应用程序就工作了。
4条答案
按热度按时间jvidinwx1#
将下面这行代码放在**onCreate()**中,而不是放在其他任何位置之前。
6ljaweal2#
在
onCreate()
之外使用SharedPreferences pref=PreferenceManager.getDefaultSharedPreferences(getBaseContext());
导致java.lang.NullPointerException
是根本原因。
因此,将代码更改为
3yhwsihp3#
你能根据自己的喜好试试吗?
pwuypxnk4#
检查此代码: