Android MediaPlayer在它自己的类中导致应用程序崩溃

时间:2016-11-06 16:58:58

标签: java android crash

我为MediaPlayer创建了一个类,以便我可以从多个类访问它,因为我需要在一次活动中启动媒体并将其停在另一个活动中。 当试图调用mediaPlayer我得到一个错误。 我不明白我在哪里出错,因为我不完全理解错误。 在为MediaPlayer创建类之前,它正在运行,但它不允许我做我想要的。 有人可以告诉我如何解决我的问题或访问MediaPlayer的不同方式。提前谢谢。

MainActivity类是我从中调用媒体播放器的地方。

public class MainActivity extends AppCompatActivity {
    Music gm = new Music();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Display the Main Activity.
        setContentView(R.layout.activity_main);

        gm.menuSound(true);
    }
}

MediaPlayer类是创建媒体播放器的位置以及启动和停止mediaPlayer的位置。

public class Music extends gameActivity{
    Context context;

    MediaPlayer menu;
    MediaPlayer soundtrack;
    MediaPlayer death;
    MediaPlayer start;

    public void menuSound(boolean x) {
        if(menu == null) {
            menu = MediaPlayer.create(context, R.raw.menumusic);
            menu.setVolume(100, 100);
        }

        if(x)
            menu.start();
        if(!x)
            menu.stop();
    }
}
  

---------崩溃的开始   E / AndroidRuntime:致命异常:主要                     处理:com.example.sam.myfirstapp,PID:2395                     java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.sam.myfirstapp / com.example.sam.myfirstapp.MainActivity}:java.lang.NullPointerException:尝试调用虚拟方法'android.content.res.Resources空对象引用上的android.content.Context.getResources()'                         在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)                         在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)                         在android.app.ActivityThread.-wrap12(ActivityThread.java)                         在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1460)                         在android.os.Handler.dispatchMessage(Handler.java:102)                         在android.os.Looper.loop(Looper.java:154)                         在android.app.ActivityThread.main(ActivityThread.java:6077)                         at java.lang.reflect.Method.invoke(Native Method)                         在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:865)                         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)                      引起:java.lang.NullPointerException:尝试在空对象引用上调用虚方法'android.content.res.Resources android.content.Context.getResources()'                         在android.media.MediaPlayer.create(MediaPlayer.java:931)                         在android.media.MediaPlayer.create(MediaPlayer.java:914)                         在com.example.sam.myfirstapp.Music.menuSound(Music.java:21)                         在com.example.sam.myfirstapp.MainActivity.onCreate(MainActivity.java:18)                         在android.app.Activity.performCreate(Activity.java:6664)                         在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)                         在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)                         在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)                         在android.app.ActivityThread.-wrap12(ActivityThread.java)                         在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1460)                         在android.os.Handler.dispatchMessage(Handler.java:102)                         在android.os.Looper.loop(Looper.java:154)                         在android.app.ActivityThread.main(ActivityThread.java:6077)                         at java.lang.reflect.Method.invoke(Native Method)                         在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:865)                         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

2 个答案:

答案 0 :(得分:0)

您的context变量为null;你永远不会设置它。

menu = MediaPlayer.create(context, R.raw.menumusic);

此外,由于您在多个活动中使用MediaPlayer,请确保使用应用程序上下文以避免上下文泄漏。

menu = MediaPlayer.create(context.getApplicationContext(), R.raw.menumusic);

答案 1 :(得分:0)

错误是使用null上下文,您尚未在音乐类中初始化上下文。

public class MainActivity extends AppCompatActivity { 
Music gm ;

@Override 
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); 

    // Display the Main Activity. 
    setContentView(R.layout.activity_main); 
      gm = new Music(this); // pass context here to music  
    gm.menuSound(true); 
}

和音乐课这样:

public class Music extends gameActivity{ 
Context context;

MediaPlayer menu;
MediaPlayer soundtrack;
MediaPlayer death;
MediaPlayer start;

 public Music(Context context){
       this.context =context; // initialising context here
      }

public void menuSound(boolean x) {
    if(menu == null) { 
        menu = MediaPlayer.create(context, R.raw.menumusic); // here context was causing null pointer exception as context was null
        menu.setVolume(100, 100); 
    } 

    if(x) 
        menu.start(); 
    if(!x) 
        menu.stop(); 
}