简体   繁体   English

Media Player示例应用程序崩溃

[英]Media Player sample app crash

I am writing an app and When I try to load the class/layout by pressing a button on my main menu, I get the following logcat errors and I do not know what they mean, can anyone tell me why my app is crashing? 我正在编写一个应用程序,当我尝试通过按主菜单上的按钮加载类/布局时,出现以下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

Code: 码:

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();
}

} }

You need to define the no argument constructor otherwise the systems doesn't know how to instantiate it. 您需要定义no参数构造函数,否则系统不知道如何实例化它。 Dalvik VM looking for a zero-argument constructor. Dalvik VM正在寻找零参数构造函数。 If you define one argument or more than one in a service. 如果在服务中定义一个或多个参数。

public Music() {
   ....
}

Android instrumentation is trying to create an instance of com.example.famouspeople.Music and for that it needs a default constructor. Android工具正在尝试创建com.example.famouspeople.Music的实例,为此,它需要一个默认的构造函数。 That is why it has failed. 这就是为什么它失败了。 Creating a default constructor, public com.example.famouspeople.Music(){} in the Music class could help. 在Music类中创建默认的构造函数public com.example.famouspeople.Music(){}可能会有所帮助。

public class Music implements OnCompletionListener{
  public Music ()
  {
  }
  public boolean  setFileDescriptor(FileDescriptor fileDescriptor){
    //set file descriptor for the current instance
  }
  public boolean   init(){
    //do initialisation of player
  }
}

call init() after setting file descriptor. 设置文件描述符后调用init() But the whole naming conventions seems confusing for your package/classes. 但是整个命名约定似乎使您的包/类感到困惑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM