尝试编写一个简单的程序。四个ImageButton,任何一次点击,特定音乐播放消息显示;再次点击任何按钮,音乐停止并再次显示消息。问题是第一次点击开始和第二次点击停止表现完美;当我按任何按钮时出现问题,此消息显示:“不幸的是程序已停止”或类似的东西,程序崩溃。
package com.example.appliedexperimentz;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity {
private MediaPlayer mp;
ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mp = new MediaPlayer();
iv = (ImageView) findViewById(R.id.spaceForAnimation);
iv.setBackgroundResource(R.drawable.animestore);
}
public void imageButton1(View v) {
if(mp.isPlaying()){
mp.release();
Toast.makeText(MainActivity.this, "Music Stopped!", Toast.LENGTH_LONG).show();
}else{
mp = MediaPlayer.create(this, R.raw.firstmusik);
mp.start();
Toast.makeText(MainActivity.this, "First Music!", Toast.LENGTH_LONG).show();
}
}
public void imageButton2(View v) {
if(mp.isPlaying()){
mp.release();
Toast.makeText(MainActivity.this, "Music Stopped!", Toast.LENGTH_LONG).show();
}else{
mp = MediaPlayer.create(this, R.raw.secondmusik);
mp.start();
Toast.makeText(MainActivity.this, "Second Music!", Toast.LENGTH_LONG).show();
}
}
public void imageButton3(View v) {
if(mp.isPlaying()){
mp.release();
Toast.makeText(MainActivity.this, "Music Stopped!", Toast.LENGTH_LONG).show();
}else{
mp = MediaPlayer.create(this, R.raw.thirdmusik);
mp.start();
Toast.makeText(MainActivity.this, "Third Music!", Toast.LENGTH_LONG).show();
}
}
public void imageButton4(View v) {
if(mp.isPlaying()){
mp.release();
Toast.makeText(MainActivity.this, "Music Stopped!", Toast.LENGTH_LONG).show();
}else{
mp = MediaPlayer.create(this, R.raw.fourthmusik);
mp.start();
Toast.makeText(MainActivity.this, "Fourth Music!", Toast.LENGTH_LONG).show();
}
}
@Override
public void onDestroy() {
mp.release();
}
}
这里是Logcat错误:
11-14 23:23:31.943:E / AndroidRuntime(659):致命异常:主要 11-14 23:23:31.943:E / AndroidRuntime(659):java.lang.IllegalStateException:无法执行活动的方法 11-14 23:23:31.943:E / AndroidRuntime(659):在android.view.View $ 1.onClick(View.java:3044) 11-14 23:23:31.943:E / AndroidRuntime(659):在android.view.View.performClick(View.java:3511) 11-14 23:23:31.943:E / AndroidRuntime(659):在android.view.View $ PerformClick.run(View.java:14105) 11-14 23:23:31.943:E / AndroidRuntime(659):在android.os.Handler.handleCallback(Handler.java:605) 11-14 23:23:31.943:E / AndroidRuntime(659):在android.os.Handler.dispatchMessage(Handler.java:92) 11-14 23:23:31.943:E / AndroidRuntime(659):在android.os.Looper.loop(Looper.java:137) 11-14 23:23:31.943:E / AndroidRuntime(659):在android.app.ActivityThread.main(ActivityThread.java:4424) 11-14 23:23:31.943:E / AndroidRuntime(659):at java.lang.reflect.Method.invokeNative(Native Method) 11-14 23:23:31.943:E / AndroidRuntime(659):at java.lang.reflect.Method.invoke(Method.java:511) 11-14 23:23:31.943:E / AndroidRuntime(659):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:784) 11-14 23:23:31.943:E / AndroidRuntime(659):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 11-14 23:23:31.943:E / AndroidRuntime(659):at dalvik.system.NativeStart.main(Native Method) 11-14 23:23:31.943:E / AndroidRuntime(659):引起:java.lang.reflect.InvocationTargetException 11-14 23:23:31.943:E / AndroidRuntime(659):at java.lang.reflect.Method.invokeNative(Native Method) 11-14 23:23:31.943:E / AndroidRuntime(659):at java.lang.reflect.Method.invoke(Method.java:511) 11-14 23:23:31.943:E / AndroidRuntime(659):在android.view.View $ 1.onClick(View.java:3039) 11-14 23:23:31.943:E / AndroidRuntime(659):......还有11个 11-14 23:23:31.943:E / AndroidRuntime(659):引起:java.lang.IllegalStateException 11-14 23:23:31.943:E / AndroidRuntime(659):在android.media.MediaPlayer.isPlaying(Native Method) 11-14 23:23:31.943:E / AndroidRuntime(659):at com.example.appliedexperimentz.MainActivity.imageButton3(MainActivity.java:59) 11-14 23:23:31.943:E / AndroidRuntime(659):... 14更多
答案 0 :(得分:0)
按如下所示更改按钮点击功能
public void releasePlayer() {
mp.release();
mp = null;
}
public void imageButton1(View v) {
if (mp != null && mp.isPlaying()) {
releasePlayer();
Toast.makeText(MainActivity.this, "Music Stopped!",
Toast.LENGTH_LONG).show();
} else {
mp = MediaPlayer.create(this, R.raw.muzika);
mp.start();
Toast.makeText(MainActivity.this, "First Music!", Toast.LENGTH_LONG)
.show();
}
}
对所有按钮点击事件进行上述更改。希望这会有所帮助:)