AudioManager 声明导致 Android 应用程序崩溃
您好,
我正在开发的一个使用 AudioManager
对象的 Android 应用程序(主要用于练习和学习目的)遇到了一些问题。但是,当按如下方式定义 AudioManager
时,应用程序会在运行时崩溃:
//Import the AudioManager
import android.media.AudioManager;
public class RingtoneModeChanger extends Activity {
//Causes crash:
public AudioManager mManager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
/* Code for the rest of the app... */
}
当我简单地注释掉 AudioManager 声明时,应用程序不会崩溃。关于我做错了什么有什么想法吗?我是 Android 开发的新手,所以我感觉我错过了一些明显的东西。
我还尝试将 android.permission.MODIFY_AUDIO_SETTINGS
和 android.permission.MODIFY_PHONE_STATE
权限添加到 AndroidManifest.xml
文件,但这有没有什么区别。
预先感谢您的任何建议!
Greetings,
I'm having a bit of trouble with an Android app that I'm working on (mostly for practice and learning purposes) that uses the AudioManager
object. When defining the AudioManager
as follows, however, the app crashes when I run it:
//Import the AudioManager
import android.media.AudioManager;
public class RingtoneModeChanger extends Activity {
//Causes crash:
public AudioManager mManager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
/* Code for the rest of the app... */
}
When I simply comment out the AudioManager declaration, the app does not crash. Any ideas as far as what I'm doing wrong? I'm a bit of a beginner to Android development, so I have a feeling that I'm missing something obvious.
I've also tried adding the android.permission.MODIFY_AUDIO_SETTINGS
and android.permission.MODIFY_PHONE_STATE
permissions to the AndroidManifest.xml
file, but that has made no difference.
Thanks in advance for any suggestions!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
想通了;在
onCreate()
方法之前无法访问系统服务。因此,我只需在onCreate()
中声明AudioManager
的对象即可。无论如何,谢谢!Figured it out; you can't access system services before the
onCreate()
method. So, I simply had to declare the object ofAudioManager
withinonCreate()
. Thanks anyway!