java.lang.NullPointerException:尝试对空对象引用调用虚拟方法“void android.media.MediaPlayer.start()”[重复]

cpjpxq1n  于 2023-03-27  发布在  Android
关注(0)|答案(3)|浏览(140)

此问题在此处已有答案

What is a NullPointerException, and how do I fix it?(12个答案)
两年前关闭了。
当我尝试点击按钮几次,然后我遇到了这个错误.下面是logcat中的错误:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.huiiy.myapplication, PID: 3933
    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.start()' on a null object reference
        at com.example.huiiy.myapplication.Activity9$13.onClick(Activity9.java:288)
        at android.view.View.performClick(View.java:5712)
        at android.view.View$PerformClick.run(View.java:22514)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6141)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

下面是我的应用程序的代码:

iv_33.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            MediaPlayer ivbsound = new MediaPlayer();
            ivbsound = MediaPlayer.create(Activity9.this,R.raw.ivbutton);
            //          MediaPlayer ivbsound = MediaPlayer.create(Activity9.this,R.raw.ivbutton);
            ivbsound.start();
            int theCard = Integer.parseInt((String) view.getTag());
            doStuff(iv_33, theCard);
        }
    });
vjhs03f7

vjhs03f71#

尝试用另一个音频文件创建播放器。可能ivbutton文件已损坏
[更新]
如果你是重复使用它声明懒惰的属性,这个声音播放器,并在每次点击播放它

class MyActivity extends AppCompatActivity {
    private MediaPlayer soundPlayer = null;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        soundPlayer = MediaPlayer.create(this, R.raw.ivbutton);

        iv_33.setOnClickListener(view -> {
            soundPlayer.start();
            int theCard = Integer.parseInt((String) view.getTag());
            doStuff(iv_33, theCard);
        });
    }
}
kpbwa7wx

kpbwa7wx2#

这意味着您的MediaPlayer的创建失败(也许您有关于它的警告/调试消息)。创建一个新的MediaPlayer然后用一个新的替换它是无用的。
检查R.raw.ivbutton是否存在并对应于良好的介质(可以运行)。
就个人而言,我是这样做的:

File mediaFile = obj.getFile();
    MediaPlayer mediaPlayer = new MediaPlayer();
    try {
        mediaFile.setReadable(true); // be sure that the program have access to the media content
        
        // this try/catch is to support multiple Android version
        try {
             mediaPlayer.setDataSource(mediaFile.getAbsolutePath());
        } catch (Exception e) {
             mediaPlayer.setDataSource(context, Uri.fromFile(mediaFile));
        }
    
        mediaPlayer.prepare(); // prepare it, after you can change volume ...
    } catch (Exception e){
        e.printStackTrace();
    }
    mediaPlayer.start(); // finally start the media
vxqlmq5t

vxqlmq5t3#

请确保您的声音文件格式,您尝试播放是支持一个检查列表从here.作为MediaPlayer.create返回空的情况下,该文件不支持,导致你空指针异常.此外,我建议检查,如果(ivbsound!=空)之前调用ivbsound.start();

相关问题