我真的不知道为什么我的媒体播放器会在我点击播放按钮时导致应用崩溃。代码如下(借用愚蠢的变量名称)。
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();
}
}
playTheSong()
方法设置为onClick for my按钮。我没有对此进行编码,而是通过设计视图中的按钮属性来完成。 R.raw.test1文件包含一个mp3文件。
对此的任何帮助都非常感谢。感谢。
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)
答案 0 :(得分:0)
您必须传递View
参数
这样做
public void playTheSong(View v){
playerOfSong = MediaPlayer.create(this, R.raw.test1);
playerOfSong.start();
}