开发者

AudioManager Declaration Crashes Android App

开发者 https://www.devze.com 2023-01-20 00:51 出处:网络
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 follow

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!


Figured it out; you can't access system services before the onCreate() method. So, I simply had to declare the object of AudioManager within onCreate(). Thanks anyway!

0

精彩评论

暂无评论...
验证码 换一张
取 消