-1

我在为我只想运行一次的注册活动构建启动画面时遇到问题。

在其他一些 stackoverflow ninjas 的帮助下,我已经能够提出以下代码:

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)&&!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();

}
}

但是,每次我执行它时-它都会强制关闭我。我是使用 SharedPreferences 的新手所以我确定我忽略了一些简单的东西。

日志猫:

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.<init> (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)
4

1 回答 1

0

在方法内部编写代码。您已经在方法之外编写了大部分代码。学习如何用java编写代码。

在 SplashScreen 类中。

public void methodName() {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this );
    ..........
    editor.commit();//This is needed or the edits will not be put into the prefs file
}
于 2013-03-18T19:30:16.540 回答