好吧,我的应用程序在启动时有一个菜单,您可以点击一个按钮来启动该应用程序。一旦它启动媒体播放器开始播放,但如果我点击后退按钮,点击按钮再次启动应用程序,然后点击按钮停止音乐应用程序崩溃。继承人的代码。它在手机上崩溃了,但在模拟器中它工作正常。任何评论将不胜感激。谢谢
int cancion = 1, cancion1 = 1;
MediaPlayer song, song1, song2, song3, song4;
if (cancion == 1)
{
song1.start();
playing = true;
song1.setLooping(true);
song = song1;
}
music.setOnLongClickListener(new View.OnLongClickListener() {
public boolean onLongClick(View view) {
song.pause();
song.seekTo(0);
playing = false;
cancion = cancion+1;
if (cancion == 5)
{
cancion = 1;
}
if (cancion == 1)
{
song1.start();
playing = true;
song1.setLooping(true);
song = song1;
}
else if (cancion == 2)
{
song2.start();
playing = true;
song2.setLooping(true);
song = song2;
}
else if (cancion == 3)
{
song3.start();
playing = true;
song3.setLooping(true);
song = song3;
}
else if (cancion == 4)
{
song4.start();
playing = true;
song4.setLooping(true);
song = song4;
}
return true;
}
});
music.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (!playing)
{
song.start();
playing = true;
}
else
{
song.pause();
song.seekTo(0);
playing = false;
cancion1 = 2;
}
}
});
@Override
protected void onResume() {
super.onResume();
if(!song.isPlaying() && cancion1 != 2)
{
song.start();
playing = true;
}
}
@Override
public void onPause(){
super.onPause();
if(song.isPlaying())
{
song.pause();
song.seekTo(0);
playing = false;
}
}
日志猫:
04-22 22:12:47.151: W/ActivityNative(12915): 发送 ACTIVITY_IDLE_TRANSACTION 04-22 22:12:47.541: W/dalvikvm(12915): threadid=1: 线程退出未捕获异常 (group=0x400207d8) 04- 22 22:12:47.551:E/AndroidRuntime(12915):致命异常:主要 04-22 22:12:47.551:E/AndroidRuntime(12915):java.lang.NullPointerException 04-22 22:12:47.551:E/ AndroidRuntime(12915): 在 com.try.player.DragActivity$5.onClick(DragActivity.java:1772) 04-22 22:12:47.551: E/AndroidRuntime(12915): 在 android.view.View.performClick(View. java:2408) 04-22 22:12:47.551: E/AndroidRuntime(12915): 在 android.view.View$PerformClick.run(View.java:8816) 04-22 22:12:47.551: E/AndroidRuntime( 12915): 在 android.os.Handler.handleCallback(Handler.java:587) 04-22 22:12:47.551: E/AndroidRuntime(12915): 在 android.os.Handler.dispatchMessage(Handler.java:92) 04 -22 22:12:47.551: E/AndroidRuntime(12915): 在 android.os.Looper.loop(Looper.java:123) 04-22 22:12:47.551: E/AndroidRuntime(12915): 在 android.app.ActivityThread.main(ActivityThread .java:4633) 04-22 22:12:47.551: E/AndroidRuntime(12915): 在 java.lang.reflect.Method.invokeNative(Native Method) 04-22 22:12:47.551: E/AndroidRuntime(12915) : 在 java.lang.reflect.Method.invoke(Method.java:521) 04-22 22:12:47.551: E/AndroidRuntime(12915): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit .java:858) 04-22 22:12:47.551: E/AndroidRuntime(12915): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 04-22 22:12:47.551: E /AndroidRuntime(12915): 在 dalvik.system.NativeStart.main(Native Method)在 android.app.ActivityThread.main(ActivityThread.java:4633) 04-22 22:12:47.551: E/AndroidRuntime(12915): 在 java.lang.reflect.Method.invokeNative(Native Method) 04-22 22: 12:47.551: E/AndroidRuntime(12915): 在 java.lang.reflect.Method.invoke(Method.java:521) 04-22 22:12:47.551: E/AndroidRuntime(12915): 在 com.android.internal .os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858) 04-22 22:12:47.551: E/AndroidRuntime(12915): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 04-22 22:12:47.551: E/AndroidRuntime(12915): at dalvik.system.NativeStart.main(Native Method)在 android.app.ActivityThread.main(ActivityThread.java:4633) 04-22 22:12:47.551: E/AndroidRuntime(12915): 在 java.lang.reflect.Method.invokeNative(Native Method) 04-22 22: 12:47.551: E/AndroidRuntime(12915): 在 java.lang.reflect.Method.invoke(Method.java:521) 04-22 22:12:47.551: E/AndroidRuntime(12915): 在 com.android.internal .os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858) 04-22 22:12:47.551: E/AndroidRuntime(12915): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 04-22 22:12:47.551: E/AndroidRuntime(12915): at dalvik.system.NativeStart.main(Native Method)java:521) 04-22 22:12:47.551: E/AndroidRuntime(12915): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858) 04-22 22:12:47.551: E/AndroidRuntime(12915): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 04-22 22:12:47.551: E/AndroidRuntime(12915): 在 dalvik.system.NativeStart.main (本机方法)java:521) 04-22 22:12:47.551: E/AndroidRuntime(12915): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858) 04-22 22:12:47.551: E/AndroidRuntime(12915): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 04-22 22:12:47.551: E/AndroidRuntime(12915): 在 dalvik.system.NativeStart.main (本机方法)