2014-12-18 5 views
0

Я разрабатываю свое собственное приложение для Android и хочу, чтобы экран заставки настраивался. Я хочу, чтобы пользователь мог включить и выключить заставку (он работает!) И установить длительность экрана заставки (небольшая проблема здесь: D). Вот моя заставка активность:Android: Длительность экрана Splash в зависимости от предпочтений

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 

Но всякий раз, когда приложение запускается, оно падает до появления всплывающего экрана. Это приложение работало до того, как я добавил параметр продолжительности. ПОМОГИТЕ!

+1

Опубликуйте LogCat –

+0

Там вы идете! Я понятия не имею, как его читать ...: x – Owais

+0

попытайтесь инициализировать 'pref' внутри конструктора' SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences (getBaseContext()); ' – Panther

ответ

0

Введите следующую строку кода внутри onCreate() и не раньше нигде.

SharedPreferences pref=PreferenceManager.getDefaultSharedPreferences(getBaseContext()); 
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); 
0

java.lang.NullPointerException вызвано SharedPreferences pref=PreferenceManager.getDefaultSharedPreferences(getBaseContext()); используется вне 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); 
} 
    } 
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 
     ... 
Смежные вопросы