gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 20:15:43 24 4
gpt4 key购买 nike

我为 MediaPlayer 创建了一个类,以便我可以从多个类访问它,因为我需要在一次 Activity 中启动媒体并在另一个 Activity 中停止它。当尝试调用 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 类是创建媒体播放器以及启动和停止媒体播放器的位置。

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();
}
}
<小时/>

--------- beginning of crash E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.sam.myfirstapp, PID: 2395 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sam.myfirstapp/com.example.sam.myfirstapp.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6077) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference at android.media.MediaPlayer.create(MediaPlayer.java:931) at android.media.MediaPlayer.create(MediaPlayer.java:914) at com.example.sam.myfirstapp.Music.menuSound(Music.java:21) at com.example.sam.myfirstapp.MainActivity.onCreate(MainActivity.java:18) at android.app.Activity.performCreate(Activity.java:6664) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)  at android.app.ActivityThread.-wrap12(ActivityThread.java)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:154)  at android.app.ActivityThread.main(ActivityThread.java:6077)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 

最佳答案

错误是由于上下文为空,您尚未在音乐类中初始化上下文。

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();
}

关于java - Android MediaPlayer 在它自己的类中导致应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40451985/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com