全局MediaPlayer声明不工作

问题描述 投票:0回答:2

每当我想声明一个本地的Mediaplayer时,它就会崩溃。我已经在本地方法中声明了,歌曲播放得很好。但是当我想全局声明它时,它就不能工作了。

package com.example.simplerecipes;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;

public class About extends Activity {


    MediaPlayer ColdplaySong;
    @Override
    protected void onCreate(Bundle savedInstanceState){

        super.onCreate(savedInstanceState);
        setContentView(R.layout.about);
        MediaPlayer.create(About.this, R.raw.coldplay );
        ColdplaySong.start();

        Thread Timer = new Thread (){

            public void run(){
                try{
                    sleep(5000);
                }catch (InterruptedException e){
                    e.printStackTrace();
                }finally{
                    Intent openMainMenu = new Intent("com.example.simplerecipes.MainActivty");
                    startActivity(openMainMenu);
                }

            }

        };
        Timer.start();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        ColdplaySong.release();
        finish();
    }

}

以下是LogCat中的错误

09-13 08:12:30.004: D/AndroidRuntime(825): Shutting down VM
09-13 08:12:30.004: W/dalvikvm(825): threadid=1: thread exiting with uncaught exception (group=0x414c4700)
09-13 08:12:30.014: E/AndroidRuntime(825): FATAL EXCEPTION: main
09-13 08:12:30.014: E/AndroidRuntime(825): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.simplerecipes/com.example.simplerecipes.About}: java.lang.NullPointerException
09-13 08:12:30.014: E/AndroidRuntime(825):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
09-13 08:12:30.014: E/AndroidRuntime(825):     at  android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
09-13 08:12:30.014: E/AndroidRuntime(825):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-13 08:12:30.014: E/AndroidRuntime(825):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
09-13 08:12:30.014: E/AndroidRuntime(825):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-13 08:12:30.014: E/AndroidRuntime(825):     at android.os.Looper.loop(Looper.java:137)
09-13 08:12:30.014: E/AndroidRuntime(825):     at android.app.ActivityThread.main(ActivityThread.java:5103)
09-13 08:12:30.014: E/AndroidRuntime(825):     at java.lang.reflect.Method.invokeNative(Native Method)
09-13 08:12:30.014: E/AndroidRuntime(825):     at java.lang.reflect.Method.invoke(Method.java:525)
09-13 08:12:30.014: E/AndroidRuntime(825):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-13 08:12:30.014: E/AndroidRuntime(825):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-13 08:12:30.014: E/AndroidRuntime(825):     at dalvik.system.NativeStart.main(Native Method)
09-13 08:12:30.014: E/AndroidRuntime(825): Caused by: java.lang.NullPointerException
09-13 08:12:30.014: E/AndroidRuntime(825):     at com.example.simplerecipes.About.onCreate(About.java:16)
09-13 08:12:30.014: E/AndroidRuntime(825):     at android.app.Activity.performCreate(Activity.java:5133)
09-13 08:12:30.014: E/AndroidRuntime(825):     at  android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
09-13 08:12:30.014: E/AndroidRuntime(825):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
09-13 08:12:30.014: E/AndroidRuntime(825):     ... 11 more       
09-13 08:12:33.154: I/Process(825): Sending signal. PID: 825 SIG: 9
java android eclipse global-variables android-mediaplayer
2个回答
2
投票
MediaPlayer ColdplaySong;

这将声明一个变量,但不初始化它(默认情况下,对象引用将是空的)。

MediaPlayer.create(About.this, R.raw.coldplay );

这将创建一个 MediaPlayer 实例,但不在任何地方存储引用。

ColdplaySong.start();

这样就会在一个空对象上调用方法,导致NullPointerException。

你可能想要的是。

ColdplaySong = MediaPlayer.create(About.this, R.raw.coldplay );
ColdplaySong.start();

0
投票

你必须用final关键字来声明MediaPlayer对象。

package com.example.simplerecipes;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;

public class About extends Activity {


  final MediaPlayer ColdplaySong =MediaPlayer.create(About.this, R.raw.coldplay );
  @Override
  protected void onCreate(Bundle savedInstanceState){

      super.onCreate(savedInstanceState);
      setContentView(R.layout.about);

      ColdplaySong.start();

      Thread Timer = new Thread (){

          public void run(){
              try{
                  sleep(5000);
              }catch (InterruptedException e){
                  e.printStackTrace();
              }finally{
                  Intent openMainMenu = new Intent("com.example.simplerecipes.MainActivty");
                  startActivity(openMainMenu);
              }

          }

      };
      Timer.start();
  }

  @Override
  protected void onPause() {
      // TODO Auto-generated method stub
      super.onPause();
      ColdplaySong.release();
      finish();
  }

}
© www.soinside.com 2019 - 2024. All rights reserved.