Note that there are some explanatory texts on larger screens.

plurals
  1. POSharedPreferences Splash Screen supposed to call Activity after loading once - force closes instead
    primarykey
    data
    text
    <p>I'm having an issue building a splash screen for a registration activity that I only want to run once. </p> <p>With the help of a few other stackoverflow ninjas I've been able to come up with the following code:</p> <pre><code>import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.content.Intent; import com.nfc.linkingmanager.R; import android.content.SharedPreferences; import java.lang.Object; import android.preference.PreferenceManager; public class SplashScreen extends Activity { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this ); finish(); if (prefs.getBoolean("SplashFlag", false)&amp;&amp;!mIsBackButtonPressed) { Intent intent = new Intent(SplashScreen.this, NewCore.class); SplashScreen.this.startActivity(intent); } else { SharedPreferences.Editor editor = preferences.edit(); editor.putBoolean("SplashFlag", true); // value to store // Again there are values for int, long, string, float, boolean Intent intent = new Intent(SplashScreen.this, AppActivity.class); SplashScreen.this.startActivity(intent); editor.commit(); // This is needed or the edits will not be put into the prefs file } private boolean mIsBackButtonPressed; private static final int SPLASH_DURATION = 1000; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.splash_screen); Handler handler = new Handler(); // run a thread after 2 seconds to start the home screen handler.postDelayed(new Runnable() { @Override public void run() { // make sure we close the splash screen so the user won't come back when it presses back key finish(); if (!mIsBackButtonPressed) { // start the home screen if the back button wasn't pressed already Intent intent = new Intent(SplashScreen.this, NewCore.class); SplashScreen.this.startActivity(intent); } } }, SPLASH_DURATION); // time in milliseconds (1 second = 1000 milliseconds) until the run() method will be called } @Override public void onBackPressed() { // set the flag to true so the next activity won't start up mIsBackButtonPressed = true; super.onBackPressed(); } } </code></pre> <p>However every time I execute it - it force closes on me. I'm new to using SharedPreferences So I'm sure I'm overlooking something simple.</p> <p>LOGCAT:</p> <pre><code>03-18 15:26:42.028: D/AndroidRuntime(23094): Shutting down VM 03-18 15:26:42.038: W/dalvikvm(23094): threadid=1: thread exiting with uncaught exception (group=0x41d42930) 03-18 15:26:42.038: E/AndroidRuntime(23094): FATAL EXCEPTION: main 03-18 15:26:42.038: E/AndroidRuntime(23094): java.lang.Error: Unresolved compilation problems: 03-18 15:26:42.038: E/AndroidRuntime(23094): Return type for the method is missing 03-18 15:26:42.038: E/AndroidRuntime(23094): This method requires a body instead of a semicolon 03-18 15:26:42.038: E/AndroidRuntime(23094): Syntax error on token ";", { expected after this token 03-18 15:26:42.038: E/AndroidRuntime(23094): preferences cannot be resolved 03-18 15:26:42.038: E/AndroidRuntime(23094): Syntax error on token "(", ; expected 03-18 15:26:42.038: E/AndroidRuntime(23094): Syntax error on token ")", ; expected 03-18 15:26:42.038: E/AndroidRuntime(23094): Syntax error on token(s), misplaced construct(s) 03-18 15:26:42.038: E/AndroidRuntime(23094): Syntax error on token "void", @ expected 03-18 15:26:42.038: E/AndroidRuntime(23094): Syntax error, insert "}" to complete ClassBody 03-18 15:26:42.038: E/AndroidRuntime(23094): at com.nfc.linkingmanager.SplashScreen.&lt;init&gt; (SplashScreen.java:16) 03-18 15:26:42.038: E/AndroidRuntime(23094): at java.lang.Class.newInstanceImpl(Native Method) 03-18 15:26:42.038: E/AndroidRuntime(23094): at java.lang.Class.newInstance(Class.java:1319) 03-18 15:26:42.038: E/AndroidRuntime(23094): at android.app.Instrumentation.newActivity(Instrumentation.java:1054) 03-18 15:26:42.038: E/AndroidRuntime(23094): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097) 03-18 15:26:42.038: E/AndroidRuntime(23094): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 03-18 15:26:42.038: E/AndroidRuntime(23094): at android.app.ActivityThread.access$600(ActivityThread.java:141) 03-18 15:26:42.038: E/AndroidRuntime(23094): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 03-18 15:26:42.038: E/AndroidRuntime(23094): at android.os.Handler.dispatchMessage(Handler.java:99) 03-18 15:26:42.038: E/AndroidRuntime(23094): at android.os.Looper.loop(Looper.java:137) 03-18 15:26:42.038: E/AndroidRuntime(23094): at android.app.ActivityThread.main(ActivityThread.java:5041) 03-18 15:26:42.038: E/AndroidRuntime(23094): at java.lang.reflect.Method.invokeNative(Native Method) 03-18 15:26:42.038: E/AndroidRuntime(23094): at java.lang.reflect.Method.invoke(Method.java:511) 03-18 15:26:42.038: E/AndroidRuntime(23094): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 03-18 15:26:42.038: E/AndroidRuntime(23094): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 03-18 15:26:42.038: E/AndroidRuntime(23094): at dalvik.system.NativeStart.main(Native Method) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload