简体   繁体   English

为什么我不能从 class 调用此方法?

[英]Why can't I call this method from a class?

I've created a class that contains a method to play a sound.我创建了一个 class,其中包含播放声音的方法。

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

}

I want to play the sound upon a button click using this code:我想使用此代码在单击按钮时播放声音:

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

When I click the button, the program crashes.当我点击按钮时,程序崩溃了。

The sound plays fine when I use code in MainActivity rather than calling a method from a separate class.当我在 MainActivity 中使用代码而不是从单独的 class 调用方法时,声音播放正常。

I suspect the issue has something to do with retrieving the sound file from the res folder, but I don't know how to fix it.我怀疑这个问题与从 res 文件夹中检索声音文件有关,但我不知道如何修复它。

I created a separate class with a method that just defines variables and I was able to call that method without any problems.我用一个只定义变量的方法创建了一个单独的 class,我能够毫无问题地调用该方法。

Logcat makes reference to this line with purple highlighting on the word "create" Logcat 引用了这一行,并在“创建”一词上以紫色突出显示

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

Does anyone know what the problem is?有谁知道问题出在哪里?

This is the stacktrace这是堆栈跟踪

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 

Thanks谢谢

The constructor of your Sounds class is not correct.您的Sounds class 的构造函数不正确。

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

}

And therefore when you try to call this Sounds class, you should do something like this:因此,当您尝试调用此Sounds class 时,您应该这样做:

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

You don't need to pass the context as part of the Sounds constructor.您不需要将上下文作为 Sounds 构造函数的一部分进行传递。 Instead I would pass the context to hydrogen.相反,我会将上下文传递给氢。 You should also make the hydrogen method public so it can be accessed from elsewhere:您还应该公开 hydrogen 方法,以便可以从其他地方访问它:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM