我有一个包含静态媒体播放器的类,但是当我尝试在另一个活动中启动媒体播放器时,程序停止工作,我在 logcat 中看不到原因。这是我的第一个活动,我从中访问静态媒体播放器:
public class MainActivity extends Activity {
TimerClass t = new TimerClass();
Button start, end, nextclass;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start = (Button) findViewById(R.id.buttStart);
end = (Button) findViewById(R.id.buttEnd);
nextclass = (Button) findViewById(R.id.buttNext);
//THIS IS WHERE I ATTEMPT TO START THE MEDIA PLAYER, PRESSING THIS BUTTON CAUSES THE PROGRAM TO STOP WORKING
start.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
TimerClass.playr.start();
}
});
end.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
TimerClass.handler.removeCallbacks(t.getRunnable());
TimerClass.num =0;
TimerClass.playr.stop();
}
});
nextclass.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent openNext = new Intent("com.example.testingtime.CLASS2");
startActivity(openNext);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
这是包含媒体播放器的类:
public class TimerClass extends Activity {
public static MediaPlayer playr;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
playr = MediaPlayer.create(TimerClass.this ,R.raw.click);
}
static int num =0;
public static Handler handler = new Handler();
public static Runnable runnable = new Runnable() {
public void run() {
playr.start();
System.out.println("Num is "+num);
num++;
handler.postDelayed(runnable, 10000);
}
};
public Runnable getRunnable(){
return runnable;
}
}
这是我的Logcat:
03-22 23:47:56.699: E/Trace(13723): error opening trace file: No such file or directory (2)
03-22 23:47:56.969: I/Adreno200-EGL(13723): <qeglDrvAPI_eglInitialize:299>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_JB_REL_RB1.04.01.01.06.043_msm7627a_JB_REL_RB1.2_Merge_release_AU (Merge)
03-22 23:47:56.969: I/Adreno200-EGL(13723): Build Date: 12/10/12 Mon
03-22 23:47:56.969: I/Adreno200-EGL(13723): Local Branch:
03-22 23:47:56.969: I/Adreno200-EGL(13723): Remote Branch: m/jb_rel_rb1.2
03-22 23:47:56.969: I/Adreno200-EGL(13723): Local Patches: NONE
03-22 23:47:56.969: I/Adreno200-EGL(13723): Reconstruct Branch: NOTHING
03-22 23:48:02.009: W/dalvikvm(13723): threadid=1: thread exiting with uncaught exception (group=0x41ce5450)
03-22 23:48:02.019: E/AndroidRuntime(13723): FATAL EXCEPTION: main
03-22 23:48:02.019: E/AndroidRuntime(13723): java.lang.NullPointerException
03-22 23:48:02.019: E/AndroidRuntime(13723): at com.example.testingtime.MainActivity$1.onClick(MainActivity.java:39)
03-22 23:48:02.019: E/AndroidRuntime(13723): at android.view.View.performClick(View.java:4129)
03-22 23:48:02.019: E/AndroidRuntime(13723): at android.view.View$PerformClick.run(View.java:17143)
03-22 23:48:02.019: E/AndroidRuntime(13723): at android.os.Handler.handleCallback(Handler.java:615)
03-22 23:48:02.019: E/AndroidRuntime(13723): at android.os.Handler.dispatchMessage(Handler.java:92)
03-22 23:48:02.019: E/AndroidRuntime(13723): at android.os.Looper.loop(Looper.java:137)
03-22 23:48:02.019: E/AndroidRuntime(13723): at android.app.ActivityThread.main(ActivityThread.java:4802)
03-22 23:48:02.019: E/AndroidRuntime(13723): at java.lang.reflect.Method.invokeNative(Native Method)
03-22 23:48:02.019: E/AndroidRuntime(13723): at java.lang.reflect.Method.invoke(Method.java:511)
03-22 23:48:02.019: E/AndroidRuntime(13723): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:813)
03-22 23:48:02.019: E/AndroidRuntime(13723): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:580)
03-22 23:48:02.019: E/AndroidRuntime(13723): at dalvik.system.NativeStart.main(Native Method)
我不确定是否有关于其他地方可能导致的信息,Logcat 是 Stacktrace 吗?
关于静态媒体播放器有一些关于 SO 的问题,我尝试实施不同的解决方案,但到目前为止没有任何效果,谁能明白为什么?