我刚接触到android并尝试修复错误发生在我的同时尝试添加带有音乐的启动到我的应用程序,下面的代码启动应用程序的三个选项开始取决于用户首选项但仍然强行关闭,
请任何帮助将不胜感激。
public class Splash extends Activity{
MediaPlayer ourSong;
@Override
protected void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
boolean without_splash_screen = getPrefs.getBoolean("without_splash_screen", true);
if (without_splash_screen == true)
{
Intent intent = new Intent(Splash.this, MainActivity.class);
startActivity(intent);}
boolean splash = getPrefs.getBoolean("splash", true);
if(splash == true) {
setContentView(R.layout.splash);
Thread timer = new Thread()
{
public void run()
{
try
{
sleep(1000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
finally
{
Intent intent = new Intent(Splash.this, MainActivity.class);
startActivity(intent);
}
}
};
timer.start();
}
boolean splash_music = getPrefs.getBoolean("splash_music", true);
if (splash_music) {
setContentView(R.layout.splash);
ourSong = MediaPlayer.create(Splash.this, R.raw.splashsound);
ourSong.start();
Thread timer = new Thread()
{
public void run()
{
try
{
sleep(1000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
finally
{
Intent intent = new Intent(Splash.this, MainActivity.class);
startActivity(intent);
}
}
};
timer.start();
}
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
ourSong.start();
finish();
}
}
logcat的:
java.lang.RuntimeException: Unable to pause activity {com.test.demo/com.test.demo.Splash}:
java.lang.NullPointerException
at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2358)
at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2315)
at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:2295)
at android.app.ActivityThread.access$1700(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:942)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.test.demo.Splash.onPause(Splash.java:89)
at android.app.Activity.performPause(Activity.java:3862)
at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1191)
at android.app.ActivityThread.performPauseActivity(ActivityThread.java:2345)
答案 0 :(得分:1)
如果our_song
为null
,splash_music
似乎false
,那么您需要更改onPause()
以检查
@Override
protected void onPause()
{
super.onPause();
if (ourSong != null)
{
ourSong.start();
}
finish();
}
虽然,我不确定你为什么在那里打start()
。您的意思是stop()
吗?