首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Splash with Music onPause问题

Splash with Music onPause问题
EN

Stack Overflow用户
提问于 2013-07-13 06:53:13
回答 1查看 201关注 0票数 0

我是android新手,尝试修复错误发生在我尝试添加音乐飞溅到我的应用程序时,下面的代码根据用户偏好启动应用程序的三个选项开始,但仍然强制关闭,

请提供任何帮助,我们将不胜感激。

代码语言:javascript
复制
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:

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-07-13 07:06:40

如果splash_musicfalse,则看起来our_songnull,因此您需要更改您的onPause()来检查这一点

代码语言:javascript
复制
  @Override
  protected void onPause() 
  {               
      super.onPause();
      if (ourSong != null)
      {
          ourSong.start();
      }
      finish();
   } 

不过,我不知道你为什么要在那里打电话给start()。你是说stop()吗?

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17625196

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档