简体   繁体   English

Android Studio Media Player崩溃的应用程序

[英]Android studio media player crashing app

I don't really know why my Media Player causes the app the crash whenever I click my play button. 我真的不知道为什么每次单击播放按钮时Media Player都会导致应用程序崩溃。 The code is below (excuse the stupid variable names). 代码如下(请使用愚蠢的变量名)。

package com.example.daud.myapplication;

import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class player_try_2 extends AppCompatActivity {
    MediaPlayer playerOfSong;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_player_try_2);
    }


    public void playTheSong(){
            playerOfSong = MediaPlayer.create(this, R.raw.test1);
            playerOfSong.start();

    }
}

The playTheSong() method is set to the onClick for my button. playTheSong()方法设置为我的按钮的onClick。 Instead of coding this in, I did it through the button properties in design view. 我没有设计代码,而是通过设计视图中的按钮属性来完成的。 The R.raw.test1 file holds an mp3 file. R.raw.test1文件保存一个mp3文件。

Any help with this is super appreciated. 对此,我们将给予任何帮助。 Thanks. 谢谢。

LogCat is showing this as the error: LogCat将其显示为错误:

07-22 00:22:52.790 11650-11650/com.example.daud.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.daud.myapplication, PID: 11650
    java.lang.IllegalStateException: Could not find method playTheSong(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'button'
        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:327)
        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)
        at android.view.View.performClick(View.java:6219)
        at android.view.View$PerformClick.run(View.java:24482)
        at android.os.Handler.handleCallback(Handler.java:769)
        at android.os.Handler.dispatchMessage(Handler.java:98)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6540)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

You must pass a View parameter 您必须传递一个View参数

Do this 做这个

public void playTheSong(View v){
        playerOfSong = MediaPlayer.create(this, R.raw.test1);
        playerOfSong.start();

}

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

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