GSYVideoPlayer icon indicating copy to clipboard operation
GSYVideoPlayer copied to clipboard

getActivityContext is null when videoView is on second display device

Open Ernest-su opened this issue 7 years ago • 3 comments

       if (getActivityContext() != null) {
            this.mContext = getActivityContext();
        } else {
            this.mContext = context;
        }

        initInflate(mContext);

        mTextureViewContainer = (ViewGroup) findViewById(R.id.surface_container);
        if (isInEditMode())
            return;
        mScreenWidth = getActivityContext().getResources().getDisplayMetrics().widthPixels;

when getting mScreenWidth, getActivityContext() can be null on second display device https://github.com/CarGuo/GSYVideoPlayer/blob/f835de08cee07580044ec8454fa6a69c6722efcd/gsyVideoPlayer-java/src/main/java/com/shuyu/gsyvideoplayer/video/base/GSYVideoView.java#L273

below is the stacktrace:

 Process: com.konka.mediadelivery, PID: 6519
    java.lang.RuntimeException: Unable to resume activity {com.konka.mediadelivery/com.konka.mediadelivery.player.PlayerActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
        at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3009)
        at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3039)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2412)
        at android.app.ActivityThread.access$800(ActivityThread.java:151)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1319)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5280)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:963)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:758)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
        at com.shuyu.gsyvideoplayer.video.base.GSYVideoView.init(GSYVideoView.java:273)
        at com.shuyu.gsyvideoplayer.video.base.GSYVideoControlView.init(GSYVideoControlView.java:197)
        at com.shuyu.gsyvideoplayer.video.base.GSYBaseVideoPlayer.init(GSYBaseVideoPlayer.java:101)
        at com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer.init(StandardGSYVideoPlayer.java:98)
        at com.shuyu.gsyvideoplayer.video.base.GSYVideoView.<init>(GSYVideoView.java:160)
        at com.shuyu.gsyvideoplayer.video.base.GSYVideoControlView.<init>(GSYVideoControlView.java:181)
        at com.shuyu.gsyvideoplayer.video.base.GSYBaseVideoPlayer.<init>(GSYBaseVideoPlayer.java:88)
        at com.shuyu.gsyvideoplayer.video.base.GSYVideoPlayer.<init>(GSYVideoPlayer.java:21)
        at com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer.<init>(StandardGSYVideoPlayer.java:89)
        at com.konka.mediadelivery.component.EmptyControlVideoView.<init>(EmptyControlVideoView.java:20)
        at com.konka.mediadelivery.component.MdVideoView.<init>(MdVideoView.kt:23)
        at com.konka.mediadelivery.component.ComponentHolder.addComponent(ComponentHolder.kt:45)
        at com.konka.mediadelivery.component.ComponentHolder.<init>(ComponentHolder.kt:35)
        at com.konka.mediadelivery.player.PlayerActivity.showPage(PlayerActivity.kt:40)
        at com.konka.mediadelivery.player.PlayerPresenter.showPage(PlayerPresenter.kt:66)
        at com.konka.mediadelivery.player.PlayerPresenter.showProgram(PlayerPresenter.kt:53)
        at com.konka.mediadelivery.player.Director.startFistProgram(Director.kt:42)
        at com.konka.mediadelivery.player.Director.start(Director.kt:25)
        at com.konka.mediadelivery.player.PlayerPresenter.onStateChanged(PlayerPresenter.kt:20)
        at androidx.lifecycle.LifecycleRegistry$ObserverWithState.dispatchEvent(LifecycleRegistry.java:361)
        at androidx.lifecycle.LifecycleRegistry.forwardPass(LifecycleRegistry.java:300)
        at androidx.lifecycle.LifecycleRegistry.sync(LifecycleRegistry.java:339)
        at androidx.lifecycle.LifecycleRegistry.moveToState(LifecycleRegistry.java:145)
        at androidx.lifecycle.LifecycleRegistry.handleLifecycleEvent(LifecycleRegistry.java:131)
        at androidx.lifecycle.ReportFragment.dispatch(ReportFragment.java:123)
        at androidx.lifecycle.ReportFragment.onResume(ReportFragment.java:90)
        at android.app.Fragment.performResume(Fragment.java:2096)
        at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:928)
        at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
        at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1049)
        at android.app.FragmentManagerImpl.dispatchResume(FragmentManager.java:1879)
        at android.app.Activity.performResume(Activity.java:6318)
        at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2997)
        at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3039) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2412) 
        at android.app.ActivityThread.access$800(ActivityThread.java:151) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1319) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:135) 
        at android.app.ActivityThread.main(ActivityThread.java:5280) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:372) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:963) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:758) 

Ernest-su avatar Jun 10 '19 09:06 Ernest-su

demo怎么复现?

CarGuo avatar Jun 10 '19 09:06 CarGuo

demo怎么复现?

这个需要设备有第二块屏幕才行。

    private fun setupSecondDisplay() {
        val displayManager = getSystemService(Context.DISPLAY_SERVICE) as DisplayManager
        val presentationDisplays = displayManager.getDisplays(DisplayManager.DISPLAY_CATEGORY_PRESENTATION)
        if (presentationDisplays.isNotEmpty()) {
            val display = presentationDisplays[0]
            secondPresentation = SecondPresentation(this, display)
            secondPresentation?.show()
        }
    }

SecondPresentation 里面放一个GSYVideoView 就会出现了。

Ernest-su avatar Jun 10 '19 10:06 Ernest-su

哦哦 Presentation 的context是比较特殊的,我这边没处理兼容

CarGuo avatar Jun 10 '19 10:06 CarGuo