我正在尝试制作一个简单的媒体播放器应用程序,该应用程序具有三个按钮,播放,暂停和停止,但是当我在模拟器上启动该应用程序时,如果单击任何按钮,应用程序都会崩溃。我无法弄清楚错误在哪里。 下面是我的.java和.xml文件...
.Java
package com.example.android.songdemo;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
MediaPlayer mp;
public void songPlay(View view){
mp.start();
}
public void songPause(View view){
mp.pause();
}
public void songStop(View view){
mp.stop();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MediaPlayer mp=MediaPlayer.create(this,R.raw.laugh);
}
}
.xml
<?xml version="1.0" encoding="utf-8"?>
<android.widget.RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.android.songdemo.MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toLeftOf="@+id/button2"
android:layout_toStartOf="@+id/button2"
android:onClick="songPlay"
android:text="play"
/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/button"
android:layout_marginEnd="65dp"
android:layout_marginRight="65dp"
android:layout_marginTop="110dp"
android:onClick="songPause"
android:text="pause"
tools:layout_editor_absoluteX="117dp"
tools:layout_editor_absoluteY="76dp" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button2"
android:layout_alignBottom="@+id/button2"
android:layout_toLeftOf="@+id/button"
android:layout_toStartOf="@+id/button"
android:onClick="songStop"
android:text="stop"
tools:layout_editor_absoluteX="117dp"
tools:layout_editor_absoluteY="145dp" />
有人可以找出错误所在吗? 下面是日志。...
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:6294)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:6294)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.start()' on a null object reference
at com.example.android.songdemo.MainActivity.songPlay(MainActivity.java:16)
答案 0 :(得分:1)
在MediaPlayer mp=MediaPlayer.create(this,R.raw.laugh);
回调中创建onCreate()
时,它将是onCreate()
回调的本地
更改:-
package com.example.android.songdemo;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
MediaPlayer mp;
public void songPlay(View view){
mp.start();
}
public void songPause(View view){
mp.pause();
}
public void songStop(View view){
mp.stop();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mp=MediaPlayer.create(this,R.raw.laugh);
}
}
答案 1 :(得分:1)
您需要删除MediaPlayer的本地初始化,就像您在本地对其进行初始化一样,因为单击时按钮的作用域将为null,因为它尚未全局初始化。
如果您在本地初始化MediaPlayer,并尝试在范围之外进行MediaPlayer的操作,则会为您提供空对象引用
package com.example.android.songdemo;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
private MediaPlayer mp;
public void songPlay(View view){
mp.start();
}
public void songPause(View view){
mp.pause();
}
public void songStop(View view){
mp.stop();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mp=MediaPlayer.create(this,R.raw.laugh); // removed the local initialization
}
}