我正在編寫一個應用程序,當我嘗試通過按下主菜單上的按鈕加載類/佈局時,出現以下logcat錯誤,我不知道它們的意思,誰能告訴我我爲什麼我的應用程序崩潰?媒體播放器示例應用程序崩潰
03-11 16:40:06.955: E/AndroidRuntime(18456): FATAL EXCEPTION: main
03-11 16:40:06.955: E/AndroidRuntime(18456): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.famouspeople/com.example.famouspeople.Music}: java.lang.InstantiationException: can't instantiate class com.example.famouspeople.Music; no empty constructor
03-11 16:40:06.955: E/AndroidRuntime(18456): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2024)
03-11 16:40:06.955: E/AndroidRuntime(18456): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
03-11 16:40:06.955: E/AndroidRuntime(18456): at android.app.ActivityThread.access$600(ActivityThread.java:140)
03-11 16:40:06.955: E/AndroidRuntime(18456): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
03-11 16:40:06.955: E/AndroidRuntime(18456): at android.os.Handler.dispatchMessage(Handler.java:99)
03-11 16:40:06.955: E/AndroidRuntime(18456): at android.os.Looper.loop(Looper.java:137)
03-11 16:40:06.955: E/AndroidRuntime(18456): at android.app.ActivityThread.main(ActivityThread.java:4898)
03-11 16:40:06.955: E/AndroidRuntime(18456): at java.lang.reflect.Method.invokeNative(Native Method)
03-11 16:40:06.955: E/AndroidRuntime(18456): at java.lang.reflect.Method.invoke(Method.java:511)
03-11 16:40:06.955: E/AndroidRuntime(18456): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
03-11 16:40:06.955: E/AndroidRuntime(18456): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
03-11 16:40:06.955: E/AndroidRuntime(18456): at dalvik.system.NativeStart.main(Native Method)
03-11 16:40:06.955: E/AndroidRuntime(18456): Caused by: java.lang.InstantiationException: can't instantiate class com.example.famouspeople.Music; no empty constructor
03-11 16:40:06.955: E/AndroidRuntime(18456): at java.lang.Class.newInstanceImpl(Native Method)
03-11 16:40:06.955: E/AndroidRuntime(18456): at java.lang.Class.newInstance(Class.java:1319)
03-11 16:40:06.955: E/AndroidRuntime(18456): at android.app.Instrumentation.newActivity(Instrumentation.java:1057)
03-11 16:40:06.955: E/AndroidRuntime(18456): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2015)
03-11 16:40:06.955: E/AndroidRuntime(18456): ... 11 more
03-11 16:40:16.560: I/Process(18456): Sending signal. PID: 18456 SIG: 9
代碼:
public class Music implements OnCompletionListener{
MediaPlayer mediaPlayer;
boolean isPrepared = false;
public Music(AssetFileDescriptor assetDescriptor){
mediaPlayer = new MediaPlayer();
try{
mediaPlayer.setDataSource(assetDescriptor.getFileDescriptor(), assetDescriptor.getStartOffset(), assetDescriptor.getLength());
mediaPlayer.prepare();
isPrepared = true;
mediaPlayer.setOnCompletionListener(this);
} catch(Exception ex){
throw new RuntimeException("Couldn't load music, uh oh!");
}
}
public Music(FileDescriptor fileDescriptor){
mediaPlayer = new MediaPlayer();
try{
mediaPlayer.setDataSource(fileDescriptor);
mediaPlayer.prepare();
isPrepared = true;
mediaPlayer.setOnCompletionListener(this);
} catch(Exception ex){
throw new RuntimeException("Couldn't load music, uh oh!");
}
}
public void onCompletion(MediaPlayer mediaPlayer) {
synchronized(this){
isPrepared = false;
}
}
public void play() {
if(mediaPlayer.isPlaying()){
return;
}
try{
synchronized(this){
if(!isPrepared){
mediaPlayer.prepare();
}
mediaPlayer.start();
}
} catch(IllegalStateException ex){
ex.printStackTrace();
} catch(IOException ex){
ex.printStackTrace();
}
}
public void stop() {
mediaPlayer.stop();
synchronized(this){
isPrepared = false;
}
}
public void switchTracks(){
mediaPlayer.seekTo(0);
mediaPlayer.pause();
}
public void pause() {
mediaPlayer.pause();
}
public boolean isPlaying() {
return mediaPlayer.isPlaying();
}
public boolean isLooping() {
return mediaPlayer.isLooping();
}
public void setLooping(boolean isLooping) {
mediaPlayer.setLooping(isLooping);
}
public void setVolume(float volumeLeft, float volumeRight) {
mediaPlayer.setVolume(volumeLeft, volumeRight);
}
public void dispose() {
if(mediaPlayer.isPlaying()){
stop();
}
mediaPlayer.release();
}
}
1)這對您沒有幫助嗎? *'不能實例化類com.example.famouspeople.Music;沒有空的構造函數* 2)如果你仍然無法弄清楚,請添加你的代碼。 – 2013-03-11 16:43:23
你不知道'沒有空的構造函數'是什麼意思? – njzk2 2013-03-11 16:43:36
@ njzk2我沒有,我只用了一週的時間學習了java,使用了教程。 – Thomas 2013-03-11 16:45:58