2013-07-12 31 views
0

即時通訊新的Android和試圖解決錯誤與我一起嘗試添加啓動音樂到我的應用程序,波紋管代碼啓動應用程序的三個選項開始取決於用戶偏好但仍然強制關閉,Splash with Music onPause問題

請任何幫助,將不勝感激。

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

我想,這是拋出異常,因爲'finish()' – Manjunath

+0

什麼是第89行? – codeMagic

回答

1

看起來our_songnull如果splash_musicfalse所以你要改變你的onPause()以檢查

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

雖然,我不知道你爲什麼在那裏打電話start()。你的意思是stop()

+0

感謝我的朋友,應用程序開始正常,你可以檢查任何複選框,但是當你選擇(splash_music)複選框,應用程序明星與飛濺和音樂,但音樂無法完成飛濺之前去下一個活動,謝謝 – androidqq6

+0

音樂必須停止與飛濺之前去到下一個活動,因爲我設置睡眠(1000); ;,但飛濺完成,並轉到下一個活動和音樂仍然在後臺運行 – androidqq6

+0

我很抱歉,但我無法理解你在說什麼。音樂不會因爲你擁有'sleep()'而停止那段時間。錯誤是在89行有一個'null',我猜是'ourSong.start();',我錯了嗎? – codeMagic