0

每当我想声明一个本地媒体播放器时,它就会崩溃。我已经在本地方法中声明,歌曲播放得非常好。但是当我想在全球范围内声明它时它不起作用。

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
4

2 回答 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();
于 2013-09-13T12:38:25.413 回答
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();
  }

}
于 2020-04-22T14:41:02.657 回答