java 为什么我不能从类调用这个方法?

tvokkenx  于 2023-01-29  发布在  Java
关注(0)|答案(2)|浏览(183)

我创建了一个类,其中包含播放声音的方法。

import android.content.Context;
import android.media.MediaPlayer;

public class Sounds {

    private Context context;

    public void Sound(Context context) {
        this.context = context;
    }

    void hydrogen() {
        final MediaPlayer mp = MediaPlayer.create(context, R.raw.hydrogen);
        mp.start();
    }

}

我想使用以下代码在单击按钮时播放声音:

public void onClick(View view){
        Sounds s = new Sounds();
      s.hydrogen();
}

当我点击按钮时,程序崩溃了。
当我在MainActivity中使用代码而不是从单独的类调用方法时,声音播放良好。
我怀疑这个问题与从res文件夹中检索声音文件有关,但我不知道如何修复它。
我创建了一个单独的类,其中包含一个只定义变量的方法,并且我能够调用该方法而没有任何问题。
Logcat引用此行时,"create"一词用紫色突出显示

final MediaPlayer mp = MediaPlayer.create(context, R.raw.hydrogen);

有人知道问题出在哪吗?
这是堆栈跟踪

E  FATAL EXCEPTION: main
                                                                                                    Process: com.xxmassdeveloper.soundtest, PID: 14132
                                                                                                    java.lang.IllegalStateException: Could not execute method for android:onClick
                                                                                                        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:473)
                                                                                                        at android.view.View.performClick(View.java:7870)
                                                                                                        at android.widget.TextView.performClick(TextView.java:14970)
                                                                                                        at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1219)
                                                                                                        at android.view.View.performClickInternal(View.java:7839)
                                                                                                        at android.view.View.access$3600(View.java:886)
                                                                                                        at android.view.View$PerformClick.run(View.java:29363)
                                                                                                        at android.os.Handler.handleCallback(Handler.java:883)
                                                                                                        at android.os.Handler.dispatchMessage(Handler.java:100)
                                                                                                        at android.os.Looper.loop(Looper.java:237)
                                                                                                        at android.app.ActivityThread.main(ActivityThread.java:7948)
                                                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                                                        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
                                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1075)
                                                                                                    Caused by: java.lang.reflect.InvocationTargetException
                                                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                                                        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:468)
                                                                                                        at android.view.View.performClick(View.java:7870) 
                                                                                                        at android.widget.TextView.performClick(TextView.java:14970) 
                                                                                                        at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1219) 
                                                                                                        at android.view.View.performClickInternal(View.java:7839) 
                                                                                                        at android.view.View.access$3600(View.java:886) 
                                                                                                        at android.view.View$PerformClick.run(View.java:29363) 
                                                                                                        at android.os.Handler.handleCallback(Handler.java:883) 
                                                                                                        at android.os.Handler.dispatchMessage(Handler.java:100) 
                                                                                                        at android.os.Looper.loop(Looper.java:237) 
                                                                                                        at android.app.ActivityThread.main(ActivityThread.java:7948) 
                                                                                                        at java.lang.reflect.Method.invoke(Native Method) 
                                                                                                        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
                                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1075) 
                                                                                                    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:1002)
                                                                                                        at android.media.MediaPlayer.create(MediaPlayer.java:981)
                                                                                                        at com.xxmassdeveloper.soundtest.Sounds.hydrogen(Sounds.java:15)
                                                                                                        at com.xxmassdeveloper.soundtest.MainActivity.onClick(MainActivity.java:48)
                                                                                                        at java.lang.reflect.Method.invoke(Native Method) 
                                                                                                        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:468) 
                                                                                                        at android.view.View.performClick(View.java:7870) 
                                                                                                        at android.widget.TextView.performClick(TextView.java:14970) 
                                                                                                        at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1219) 
                                                                                                        at android.view.View.performClickInternal(View.java:7839) 
                                                                                                        at android.view.View.access$3600(View.java:886) 
                                                                                                        at android.view.View$PerformClick.run(View.java:29363) 
                                                                                                        at android.os.Handler.handleCallback(Handler.java:883) 
                                                                                                        at android.os.Handler.dispatchMessage(Handler.java:100) 
                                                                                                        at android.os.Looper.loop(Looper.java:237) 
                                                                                                        at android.app.ActivityThread.main(ActivityThread.java:7948) 
                                                                                                        at java.lang.reflect.Method.invoke(Native Method) 
                                                                                                        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
                                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1075) 
2023-01-27 10:29:00.836 14132-14132 Process                 com.xxmassdeveloper.soundtest        I  Sending signal. PID: 14132 SIG: 9
2023-01-27 10:29:00.912  3675-4184  WindowManager           pid-3675                             E  win=Window{57fa696 u0 com.xxmassdeveloper.soundtest/com.xxmassdeveloper.soundtest.MainActivity EXITING} destroySurfaces: appStopped=false win.mWindowRemovalAllowed=true win.mRemoveOnExit=true win.mViewVisibility=0 caller=com.android.server.wm.AppWindowToken.destroySurfaces:1200 com.android.server.wm.AppWindowToken.destroySurfaces:1181 com.android.server.wm.WindowState.onExitAnimationDone:5030 com.android.server.wm.-$$Lambda$01bPtngJg5AqEoOWfW3rWfV7MH4.accept:2 java.util.ArrayList.forEach:1262 com.android.server.wm.AppWindowToken.onAnimationFinished:3549 com.android.server.wm.AppWindowToken.commitVisibility:860

谢谢

fumotvh3

fumotvh31#

Sounds类的构造函数不正确。

public class Sounds {

    private Context context;

    // You should have a constructor here, not a function
    // So, void should be removed. And you should have name same as the class name
    public Sounds(Context context) {
        this.context = context;
    }

    // Better to make is public so that you can call it in other packages
    public void hydrogen() {
        final MediaPlayer mp = MediaPlayer.create(context, R.raw.hydrogen);
        mp.start();
    }

}

因此,当您尝试调用Sounds类时,您应该执行以下操作:

public void onClick(View view) {
    // As you are inside an Activity, you can pass this
    Sounds s = new Sounds(this);
    s.hydrogen();
}
krcsximq

krcsximq2#

你不需要把上下文作为Sounds构造函数的一部分传递,而是把上下文传递给hydrogen,你还应该把hydrogen方法设置为public,这样就可以从其他地方访问它:

public void hydrogen(Context context)
{
     final MediaPlayer mp = MediaPlayer.create(context, R.raw.hydrogen);
     mp.start();
}

相关问题