制作启动画面时遇到一些问题。它开始很好但后来进入下一个活动并在定时动画后崩溃。这是我的代码:
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
答案 0 :(得分:1)
尝试使用
Intent i = new Intent(SplashScreen.this, Main.class);
答案 1 :(得分:0)
你在线程中调用getApplicationContext()
。
尝试更换:
Intent i = new Intent(getApplicationContext(), Main.class);
使用:
Intent i = new Intent(SplashScreen.this, Main.class);