0

我是 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();
              } 
            }

日志猫:

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)
4

1 回答 1

1

看起来our_songnull这样splash_musicfalse所以你会想要改变你onPause()的检查

  @Override
  protected void onPause() 
  {               
      super.onPause();
      if (ourSong != null)
      {
          ourSong.start();
      }
      finish();
   } 

虽然,我不确定你为什么打电话给start()那里。你的意思是stop()

于 2013-07-12T23:06:40.490 回答