MediaPlayer MediaController Service

I can not fix this problem:

android.view.WindowManager $ BadTokenException: Unable to add window zero token; Does your activity work?

I have an application that has a hosting with a fragment that MediaController has. This activity is associated with the Service, which hosts the media player. What I want to do is start playing the song from step A, show the mediacontroller. Then leave step A and enter activity B, reconnect to the service, and if the sound is still playing, show the MediaController in step B. I keep getting the error above, no matter what I do. Actions A and B are the same different instances of the same activity. Let me know if you need to see any other code.

 private ServiceConnection callConnection = new ServiceConnection() {

    @Override
    public void onServiceConnected( ComponentName name, IBinder service ) {
        Log.d("LEAKTEST", "Connected to instance " + this.toString());
        AudioService.MusicBinder binder = ( AudioService.MusicBinder ) service;
        callService = binder.getService();
        callService.setPreparedCallback( SpeciesDetailsFragment.this );
        callService.setList( callList );
        callService.setSpeciesId( getSpeciesId() );
        if ( callService.isPng() ) {
            setController();
            showController();
        }
        musicBound = true;
    }

    @Override
    public void onServiceDisconnected( ComponentName name ) {
        musicBound = false;
    }
};

private void setController() {
    if ( controller == null ) {
        controller = new CallController( getActivity() );
    }
    controller.setPrevNextListeners( new View.OnClickListener() {
        @Override
        public void onClick( View v ) {
            playNext();
        }
    }, new View.OnClickListener() {
        @Override
        public void onClick( View v ) {
            playPrev();
        }
    } );
    controller.setMediaPlayer( this );
    controller.setAnchorView( rootView );
}

private void showController(){
    controller.show(0);
    controller.setEnabled( true );
}

   @Override
public void onPause() {
    controller.hide();
    loadingDialog.hide();
    super.onPause();
}

@Override
public void onStop() {
    controller.hide();
    controller = null;
    // Unbind from the service
    if ( musicBound ) {
        getActivity().unbindService( callConnection );
        musicBound = false;
    }
    super.onStop();
}

:

06-03 19:38:11.986  17417-17417/com.myname.appname.core E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.myname.appname.core, PID: 17417
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
        at android.view.ViewRootImpl.setView(ViewRootImpl.java:536)
        at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:259)
        at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
        at android.widget.MediaController.show(MediaController.java:346)
        at com.myname.appname.core.details.SpeciesDetailsFragment.showController(SpeciesDetailsFragment.java:354)
        at com.myname.appname.core.details.SpeciesDetailsFragment.access$700(SpeciesDetailsFragment.java:49)
        at com.myname.appname.core.details.SpeciesDetailsFragment$6.onServiceConnected(SpeciesDetailsFragment.java:382)
        at android.app.LoadedApk$ServiceDispatcher.doConnected(LoadedApk.java:1117)
        at android.app.LoadedApk$ServiceDispatcher$RunConnection.run(LoadedApk.java:1134)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5144)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:611)
        at dalvik.system.NativeStart.main(Native Method)

,

+4
1

showController() - , , .

, bindService() Fragment. onActivityCreated() , , Activity . .

, onServiceConnected(), , . - onStop() , .

, , :

callService.setPreparedCallback(SpeciesDetailsFragment.this);

. UI, , . , -.

+2

Source: https://habr.com/ru/post/1542834/


All Articles