frameworks/base/media/java/android/media/MediaPlayer.java:670: error: constructor PlayerBase in class PlayerBase cannot be applied to given types; public MediaPlayer() { ^ required: AudioAttributes,int found: no arguments reason: actual and formal argument lists differ in length frameworks/base/media/java/android/media/MediaPlayer.java:672: error: call to this must be first statement in constructor this(AudioSystem.AUDIO_SESSION_ALLOCATE); ^ 2 errors怎么办
时间: 2024-03-08 18:48:02 浏览: 45
这是一个编译错误,错误信息显示 `PlayerBase` 类的构造函数参数个数不匹配。这是因为 `MediaPlayer` 类的构造函数调用了 `PlayerBase` 类的构造函数,并传递了错误的参数个数或类型。解决这个问题的方法是修改 `MediaPlayer` 类的构造函数,使其调用正确的 `PlayerBase` 类的构造函数,并传递正确的参数。
具体来说,可以在 `MediaPlayer` 类的构造函数中调用 `PlayerBase` 类的构造函数,并传递一个 `AudioAttributes` 对象和一个整数值作为参数。这样就可以解决上述编译错误。修改后的代码如下:
```
public MediaPlayer() {
super(new AudioAttributes.Builder().build(), 0);
this(AudioSystem.AUDIO_SESSION_ALLOCATE);
}
```
在这个修改后的构造函数中,首先调用了 `PlayerBase` 类的构造函数,并传递了一个 `AudioAttributes` 对象和一个整数值作为参数,然后再调用了 `MediaPlayer` 类的另一个构造函数,并传递了一个整数值作为参数。这样就可以正确地创建一个 `MediaPlayer` 对象了。
相关问题
行 35674: frameworks/base/media/java/android/media/MediaPlayer.java:670: error: constructor PlayerBase in class PlayerBase cannot be applied to given types; 行 35680: frameworks/base/media/java/android/media/MediaPlayer.java:672: error: call to this must be first statement in constructor
这两个错误都是在 MediaPlayer.java 文件中出现的 Java 编译错误。
第一个错误是在第 35674 行,错误提示是在 PlayerBase 类中的构造函数 PlayerBase 无法应用于给定的类型。这个错误通常是因为你在实例化 PlayerBase 的时候,传递了错误的参数或者参数类型不匹配。你需要检查传递给 PlayerBase 构造函数的参数类型是否与 PlayerBase 构造函数的参数类型匹配。
第二个错误是在第 35680 行,错误提示是在构造函数中调用 this 必须放在构造函数的第一行。这个错误通常是因为你在构造函数中调用了其他方法或构造函数,而这些调用不是构造函数的第一个语句。要解决这个问题,你需要将调用 this 的语句移到构造函数的第一行,或者使用 super 调用父类的构造函数作为第一个语句。
例如,你可以将 this(AudioSystem.AUDIO_SESSION_ALLOCATE) 调用移到构造函数的第一行,如下所示:
```java
public MediaPlayer() {
this(AudioSystem.AUDIO_SESSION_ALLOCATE);
// other constructor code
}
private MediaPlayer(int audioSession) {
// constructor code
}
```
这样,就可以避免 "call to this must be first statement in constructor" 错误。
frameworks/base/media/java/android/media/MediaPlayer.java:672: error: call to this must be first statement in constructor this(AudioSystem.AUDIO_SESSION_ALLOCATE);
这个错误是 Java 编译器的错误提示,意思是构造函数中调用 this 必须放在构造函数的第一行。在 MediaPlayer.java 文件的第 672 行,构造函数调用了 this(AudioSystem.AUDIO_SESSION_ALLOCATE) 方法,但是该调用不是构造函数的第一行,所以导致了编译错误。
要解决这个问题,你需要将 this(AudioSystem.AUDIO_SESSION_ALLOCATE) 方法调用移到构造函数的第一行,或者使用 super 调用父类的构造函数作为第一个语句。下面是一个示例:
```java
public MediaPlayer() {
this(AudioSystem.AUDIO_SESSION_ALLOCATE);
// other constructor code
}
private MediaPlayer(int audioSession) {
// constructor code
}
```
在这个示例中,构造函数调用了 this(AudioSystem.AUDIO_SESSION_ALLOCATE) 方法,但是该方法调用被移动到了构造函数的第一行,解决了编译错误。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)