2012-01-10 142 views
0

我在启动屏幕时遇到了一些问题。它开始非常好,但随后进入下一个活动并在定时动画后崩溃。这里是我的代码:初始屏幕崩溃

public class SplashScreen extends Activity { 
final static int DURATION = 2000; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.splash); 
} 

@Override 
protected void onResume() { 
    super.onResume(); 
    splashWelcome(DURATION); 
} 

//Run the splash screen for given time limit 
protected void splashWelcome(final int limit) { 
    Thread splashThread = new Thread() { 
     @Override 
     public void run() { 
      try { 
       int waited = 0; 
       while (waited < limit) { 
        sleep(100); 
        waited += 100; 
       } 
      } catch (InterruptedException e) { 
       Log.d("SplashScreen Error:", e.getMessage().toString()); 
      } finally { 
       Intent i = new Intent(getApplicationContext(), Main.class); 
       startActivity(i); 
       finish(); 
      } 

     } 
    }; 
    splashThread.start(); 
} 

}

这是错误:

01-10 12:23:57.835: ERROR/AndroidRuntime(19092): FATAL EXCEPTION: Thread-10 
01-10 12:23:57.835: ERROR/AndroidRuntime(19092): java.lang.NullPointerException 
01-10 12:23:57.835: ERROR/AndroidRuntime(19092):  at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:100) 
01-10 12:23:57.835: ERROR/AndroidRuntime(19092):  at com.fab.quotes.SplashScreen$1.run(SplashScreen.java:36) 
01-10 12:23:57.835: WARN/ActivityManager(114): Force finishing activity com.fab.quotes/.Main 
+1

不应这是意图I =新意图(this.class,Main.class);?你尝试过吗?它的工作原理是 – kosa 2012-01-10 18:49:16

回答

1

尝试使用

Intent i = new Intent(SplashScreen.this, Main.class); 
0

你调用getApplicationContext()一个线程内。

尝试更换:

Intent i = new Intent(getApplicationContext(), Main.class); 

随着:

Intent i = new Intent(SplashScreen.this, Main.class); 
+0

。大!同时,为什么我不能使用方法getApplicationContext(),因为它一直工作(虽然我从来没有在线程内部调用过它) – faby 2012-01-12 02:07:15

+0

我认为这是因为Application对象以及所有其他顶级类(如Activity)都是由主线程初始化,因此在单独的线程中访问它们将返回空/崩溃。但我不能确定。我总是使用'class.this',因为我遇到了几个使用'getApplicationContext()'返回null的问题。你会在这里看到你不应该真的做getApplicationContext调用,除非你必须:http://developer.android.com/reference/android/content/Context.html#getApplicationContext%28%29。 – Ricky 2012-01-12 11:26:22