Java SharedReferences启动屏幕应该在加载一次后调用活动-强制关闭

Java SharedReferences启动屏幕应该在加载一次后调用活动-强制关闭,java,android,eclipse,sharedpreferences,Java,Android,Eclipse,Sharedpreferences,我在为注册活动构建初始屏幕时遇到了一个问题,我只想运行一次 在其他几个stackoverflow忍者的帮助下,我已经能够想出以下代码: import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.content.Intent; import com.nfc.linkingmanager.R; import android.content.SharedPrefere

我在为注册活动构建初始屏幕时遇到了一个问题,我只想运行一次

在其他几个stackoverflow忍者的帮助下,我已经能够想出以下代码:

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

}
}
然而,每当我执行它时,它的力量就会向我逼近。我不熟悉使用SharedReference,所以我肯定我忽略了一些简单的东西

日志:

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)
03-18 15:26:42.028:D/AndroidRuntime(23094):关闭虚拟机
03-18 15:26:42.038:W/dalvikvm(23094):threadid=1:线程退出时出现未捕获异常(组=0x41d42930)
03-18 15:26:42.038:E/AndroidRuntime(23094):致命异常:主
03-18 15:26:42.038:E/AndroidRuntime(23094):java.lang.Error:未解决的编译问题:
03-18 15:26:42.038:E/AndroidRuntime(23094):缺少方法的返回类型
03-18 15:26:42.038:E/AndroidRuntime(23094):此方法需要正文而不是分号
03-18 15:26:42.038:E/AndroidRuntime(23094):标记“;”上的语法错误,{应在该标记之后
03-18 15:26:42.038:E/AndroidRuntime(23094):无法解析首选项
03-18 15:26:42.038:E/AndroidRuntime(23094):令牌“(”上的语法错误;应为
03-18 15:26:42.038:E/AndroidRuntime(23094):令牌“,”上的语法错误;应为
03-18 15:26:42.038:E/AndroidRuntime(23094):令牌语法错误,构造错误
03-18 15:26:42.038:E/AndroidRuntime(23094):令牌“void”上的语法错误,@expected
03-18 15:26:42.038:E/AndroidRuntime(23094):语法错误,插入“}”以完成类正文
03-18 15:26:42.038:E/AndroidRuntime(23094):在com.nfc.linkingmanager.SplashScreen上。(SplashScreen.java:16)
03-18 15:26:42.038:E/AndroidRuntime(23094):位于java.lang.Class.newInstanceImpl(本机方法)
03-18 15:26:42.038:E/AndroidRuntime(23094):位于java.lang.Class.newInstance(Class.java:1319)
03-18 15:26:42.038:E/AndroidRuntime(23094):在android.app.Instrumentation.newActivity(Instrumentation.java:1054)上
03-18 15:26:42.038:E/AndroidRuntime(23094):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
03-18 15:26:42.038:E/AndroidRuntime(23094):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-18 15:26:42.038:E/AndroidRuntime(23094):在android.app.ActivityThread.access$600(ActivityThread.java:141)
03-18 15:26:42.038:E/AndroidRuntime(23094):在android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-18 15:26:42.038:E/AndroidRuntime(23094):在android.os.Handler.dispatchMessage(Handler.java:99)上
03-18 15:26:42.038:E/AndroidRuntime(23094):在android.os.Looper.loop(Looper.java:137)
03-18 15:26:42.038:E/AndroidRuntime(23094):在android.app.ActivityThread.main(ActivityThread.java:5041)上
03-18 15:26:42.038:E/AndroidRuntime(23094):位于java.lang.reflect.Method.Invokenactive(本机方法)
03-18 15:26:42.038:E/AndroidRuntime(23094):位于java.lang.reflect.Method.invoke(Method.java:511)
03-18 15:26:42.038:E/AndroidRuntime(23094):在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-18 15:26:42.038:E/AndroidRuntime(23094):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-18 15:26:42.038:E/AndroidRuntime(23094):在dalvik.system.NativeStart.main(本机方法)

在方法内部编写代码。您已经编写了该方法之外的大多数代码。学习如何用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
}

你的代码看起来很奇怪。OnCreate部分看起来不错,但“公共类SplashScreen扩展活动{”之后的部分是WTFIt的上帝告诉你SplashScreen是邪恶的。lol!@Simon我按照建议更新了它……但它仍然在关闭。:(当你启动活动时,Android调用OnCreate()之后是活动生命周期中记录的一系列方法。仅在这些方法中,或在这些方法内部调用的方法中,或在回调(例如onClickListener)中运行的方法中执行代码是执行的。你不能只是将代码放入类中,然后以某种神奇的方式期待它被执行。你必须告诉Android要执行什么。你能告诉我怎么做吗?(我被卡住了。)我更新了代码…没有更多的强制关闭错误!但是,应用程序似乎最小化了(在我的日志中没有输出)@user2163126您的代码仍然完全处于非托管状态,请阅读它自己的代码。您认为非托管是什么意思?(我添加了您建议的内容-还有其他内容需要添加吗?)帮助!!:)我的最后期限已经到了…我最后要做的就是让这个启动屏幕正常工作:(