-1

I have a music player application and untill now, it was working smoothly.

So, i learnt about Android Focus from android documentation and i thought i would try this on my app for learning purposes.

App: minimum api level :16; Maximum:26;

I basically implemented the AudioManager.OnAudioFocusChangeListener in my service class.

In it i included this code:

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

        }

My onAudioFocusChange(int focusChange) looks something like this:

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

I tried implementing audiofocus in my application and this is how i did it. The issue is when i try to run my app now , it gives me this error

unable to create a service

What's the problem with it? i think i read the documentation clearly.

Am i missing out something?

Update:

Removing the below code does make it work

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

Stacktrace:

     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

Tilak Raj
  • 1,369
  • 5
  • 31
  • 64
  • *What's the problem with it?* - you are supposed to tell us. We are not wizards who can magically guess problems – Tim Dec 18 '17 at 10:41
  • I can't figure out, one thing that i saw was that as i comment out `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(); }` this part , it does work. – Tilak Raj Dec 18 '17 at 10:45
  • I have no clue what you are saying. You have some code and it works, or maybe not, perhaps you have an error, no clue. You are supposed to tell us what your issue is – Tim Dec 18 '17 at 10:46
  • there is no need to be rude and ignorant. I tried impementing `audiofocus` in my application and this is how i did it. The issue is when i try to run my app now , it gives me this error `unable to create a service` what is so hard to understand? – Tilak Raj Dec 18 '17 at 10:48
  • I'm not ignorant, you posted half a question. I have to comment twice before you even tell what the error is, and there is no stacktrace either. Please read [ask] and provide some detail – Tim Dec 18 '17 at 10:52
  • @TimCastelijns does it help now? let me post the stack trace too – Tilak Raj Dec 18 '17 at 10:53
  • @TimCastelijns please consider my question again. – Tilak Raj Dec 18 '17 at 11:08
  • I don't understand. You don't know what a nullpointerexception is, or is there another issue? – Tim Dec 18 '17 at 11:14
  • `brother` , `Song songPlay=songs.get(songposn);`: this is the line that throws that exception. Why did it do ? because it didn't create an instance of the service class in the starting place.Hence coming to the same point again. I know what is a null pointer exception is – Tilak Raj Dec 18 '17 at 11:16
  • @TimCastelijns i dont think there was a point to downvote it and close it.. :) – Tilak Raj Dec 18 '17 at 11:17
  • it is not for you to decide how other people vote on your question. Good luck fixing your issue. I suggest you fix that NPE before continuing. – Tim Dec 18 '17 at 11:21
  • i will through my bounty tommorow , lets see if it helps. Thanks for the help though. i appreciate it. – Tilak Raj Dec 18 '17 at 12:08
  • Duplicate of: https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it – Stephen C Dec 21 '17 at 02:51
  • @StephenC seriously downvote? The service didn't get created in the first place, hence the error – Tilak Raj Dec 21 '17 at 02:52
  • Yes. Downvote. This is a classical NPE, and the solution is self-evident; i.e. you haven't done adequate research. (Starting with ... not reading the stacktrace properly ...) Furthermore, your Q will be of zero use to any other reader. If you hadn't posted a bonus, I would have closed it as a Duplicate. – Stephen C Dec 21 '17 at 04:40
  • Then there is the problem that the Q doesn't contain a proper MCVE. The code where the real problem is in a pastebin link. That's a big " no no " on StackOverflow. – Stephen C Dec 21 '17 at 04:46

2 Answers2

1

look your Error Msg!this is a NullPointerException. List is a null object! check you code

 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
Martin Yu
  • 271
  • 2
  • 3
  • 11
1

The problem is that your application does not initialise songs. (Sure, there is a setSongs method, but you don't call it!)

Solution: add code to initialize songs in the appropriate place. That will fix this error ...

I strongly advise you to read the Question and Answers for:

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216