无法创建服务

时间:2017-12-17 20:23:10

标签: android

我有一个音乐播放器应用程序,直到现在,它运作顺利。

所以,我从android文档中了解了Android Focus,我想我会在我的应用程序上尝试这个用于学习目的。

应用:最低api级别:16;最大:26;

我基本上在我的服务类中实现了AudioManager.OnAudioFocusChangeListener

在其中我包含了这段代码:

if(Build.VERSION.SDK_INT>=26){
            mAudioFocusRequest = new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN)
                    .setAudioAttributes(mAudioAttributes)
                    .setAcceptsDelayedFocusGain(true)
                    .setOnAudioFocusChangeListener(this)
                    .build();
            int res = mAudioManager.requestAudioFocus(mAudioFocusRequest);
            synchronized(mFocusLock) {
                if (res == AudioManager.AUDIOFOCUS_REQUEST_FAILED) {
                    mPlaybackNowAuthorized = false;
                } else if (res == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
                    mPlaybackNowAuthorized = true;
                    playSong();
                } else if (res == AudioManager.AUDIOFOCUS_REQUEST_DELAYED) {
                    mPlaybackDelayed = true;
                    mPlaybackNowAuthorized = false;
                }
            }

        } else if(Build.VERSION.SDK_INT<26){
            int result = 0;
            if (mAudioManager != null) {
                result = mAudioManager.requestAudioFocus(this,AudioManager.STREAM_MUSIC,AudioManager.AUDIOFOCUS_GAIN);
            }

            if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
                mPlaybackNowAuthorized = true;
                playSong();
            }

        }

我的onAudioFocusChange(int focusChange)看起来像这样:

if(Build.VERSION.SDK_INT<26){
            //<26 code
        }else {
           //26 and above api level
    }

我尝试在我的应用程序中实现audiofocus,这就是我做到的。问题是当我尝试现在运行我的应用程序时,它给了我这个错误

  

无法创建服务

它有什么问题?我想我清楚地阅读了文件。

我错过了什么吗?

更新:

删除以下代码确实可以正常工作

 else if(Build.VERSION.SDK_INT<26){
        int result = 0;
        if (mAudioManager != null) {
            result = mAudioManager.requestAudioFocus(this,AudioManager.STREAM_MUSIC,AudioManager.AUDIOFOCUS_GAIN);
        }

        if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
            mPlaybackNowAuthorized = true;
            playSong();
        }

堆栈跟踪:

     12-18 16:27:47.740 30183-30183/com.example.tilak.imusicplay E/AndroidRuntime: 
12-18 16:27:47.740 30183-30183/com.example.tilak.imusicplay E/AndroidRuntime: FATAL EXCEPTION: main
                                                                              Process: com.example.tilak.imusicplay, PID: 30183
                                                                              java.lang.RuntimeException: Unable to create service com.example.tilak.imusicplay.MusicService: java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object java.util.List.get(int)' on a null object reference
                                                                                  at android.app.ActivityThread.handleCreateService(ActivityThread.java:2920)
                                                                                  at android.app.ActivityThread.access$2000(ActivityThread.java:153)
                                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1458)
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                  at android.os.Looper.loop(Looper.java:154)
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:5529)
                                                                                  at java.lang.reflect.Method.invoke(Native Method)
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)
                                                                               Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object java.util.List.get(int)' on a null object reference
                                                                                  at com.example.tilak.imusicplay.MusicService.playSong(MusicService.java:239)
                                                                                  at com.example.tilak.imusicplay.MusicService.initMusicPlayer(MusicService.java:115)
                                                                                  at com.example.tilak.imusicplay.MusicService.onCreate(MusicService.java:66)
                                                                                  at android.app.ActivityThread.handleCreateService(ActivityThread.java:2910)
                                                                                  at android.app.ActivityThread.access$2000(ActivityThread.java:153) 
                                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1458) 
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                  at android.os.Looper.loop(Looper.java:154) 
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:5529) 
                                                                                  at java.lang.reflect.Method.invoke(Native Method) 
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739) 
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629) 
12-18 16:27:47.742 30183-30183/com.example.tilak.imusicplay E/MQSEventManagerDelegate: failed to get MQSService.

Whole Class Code

2 个答案:

答案 0 :(得分:1)

查看你的错误消息!这是一个NullPointerException。 List是一个null对象!检查你的代码

 java.lang.RuntimeException: Unable to create service com.example.tilak.imusicplay.MusicService: java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object java.util.List.get(int)' on a null object

答案 1 :(得分:1)

问题是您的应用程序未初始化songs。 (当然,有一个setSongs方法,但你没有调用它!)

解决方案:添加代码以在适当的位置初始化songs。这将修复错误......

我强烈建议您阅读以下问题和答案: