我有3个按钮,当按下按钮时会播放声音。但由于某种原因,我现在开始得到这个错误,在我实现了所有3个按钮之后。当我只做一个按钮时,它播放的声音没有错误。在我实施了2个之后,应用程序开始崩溃
这是我的.xml
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Play!"
android:id="@+id/play1"
android:layout_below="@+id/imageView"
android:layout_toLeftOf="@+id/play3"
android:layout_toStartOf="@+id/play3" />
这是mainactivity.java
// final MediaPlayer mp = MediaPlayer.create(this, R.raw.sounds1);
// final MediaPlayer mp2 = MediaPlayer.create(this, R.raw.sounds2);
// final MediaPlayer mp3 = MediaPlayer.create(this, R.raw.sounds3);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Button play_button = (Button)this.findViewById(R.id.play1);
//// Button play_button2 = (Button)this.findViewById(R.id.play2);
//// Button play_button3 = (Button)this.findViewById(R.id.play3);
//
// play_button.setOnClickListener(new View.OnClickListener() {
// public void onClick(View v) {
//
//
// mp.start();
// }
// });
//
// play_button2.setOnClickListener(new View.OnClickListener() {
// public void onClick(View v) {
//
//
// mp2.start();
// }
// });
//
// play_button3.setOnClickListener(new View.OnClickListener() {
// public void onClick(View v) {
//
//
// mp3.start();
// }
// });
}
这也是日志!
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.sounds.apps.sounds/com.sounds.apps.sounds.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
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.content.ContextWrapper.getResources(ContextWrapper.java:85)
at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:74)
at android.media.MediaPlayer.create(MediaPlayer.java:919)
at android.media.MediaPlayer.create(MediaPlayer.java:902)
at com.sounds.apps.sounds.MainActivity.<init>(MainActivity.java:14)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.Class.newInstance(Class.java:1572)
at android.app.Instrumentation.newActivity(Instrumentation.java:1065)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2199)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
答案 0 :(得分:0)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final MediaPlayer mp = MediaPlayer.create(this, R.raw.sounds1);
final MediaPlayer mp2 = MediaPlayer.create(this, R.raw.sounds2);
final MediaPlayer mp3 = MediaPlayer.create(this, R.raw.sounds3);
Button play_button = (Button)this.findViewById(R.id.play1);
Button play_button2 = (Button)this.findViewById(R.id.play2);
Button play_button3 = (Button)this.findViewById(R.id.play3);
play_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mp.start();
}
});
play_button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mp2.start();
}
});
play_button3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mp3.start();
}
});
}