0

Im new to android and trying to fix error happend with me while try to add splash with music to my app , bellow the code to start three option begining of app depend on user Preference but still get force closed ,

please any help will be appreciated .

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

1 に答える 1

1

のようour_songに見えるので、それを確認するために変更する必要がありますnullsplash_musicfalseonPause()

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

とはいえ、なぜあなたがstart()そこに電話をかけているのかわかりません。ということstop()ですか?

于 2013-07-12T23:06:40.490 に答える