VideoCastManager 在手机启动时崩溃 Android 应用程序

Posted

技术标签:

【中文标题】VideoCastManager 在手机启动时崩溃 Android 应用程序【英文标题】:VideoCastManager crashes Android app on Phone boot 【发布时间】:2016-04-07 20:07:55 【问题描述】:

我创建了一个简单的应用程序,它使用CastCompanionLibrary 将视频文件流式传输到 Google ChromeCast 设备。一切似乎都很好,除了我发现在没有互联网连接的情况下启动手机时应用程序崩溃。我尝试在 ADB 监视器上捕获错误。

java.lang.RuntimeException: Unable to start receiver com.google.android.libraries.cast.companionlibrary.remotecontrol.VideoIntentReceiver: java.lang.IllegalStateException: No VideoCastManager instance was found, did you forget to initialize it?

让我感到奇怪的是,为什么我的应用程序在手机启动时就启动了?它应该仅在用户启动时启动。

但是经过一番挖掘,我相信这与前面提到的 CastCompanionLibrary 有关,它使用清单文件注册了一些服务和接收器。

Manifest.xml 部分

<activity
        android:name="com.google.android.libraries.cast.companionlibrary.cast.player.VideoCastControllerActivity"
        android:screenOrientation="portrait"
        android:label="@string/app_name"
        android:launchMode="singleTask"
        android:parentActivityName="lv.test.myapp.MainActivity"
        android:theme="@style/Theme.AppCompat.Light.NoActionBar">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="lv.test.myapp.MainActivity" />
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
        </intent-filter>
    </activity>
    <receiver android:name="com.google.android.libraries.cast.companionlibrary.remotecontrol.VideoIntentReceiver" >
        <intent-filter>
            <action android:name="android.media.AUDIO_BECOMING_NOISY" />
            <action android:name="android.intent.action.MEDIA_BUTTON" />
            <action android:name="com.google.android.libraries.cast.companionlibrary.action.toggleplayback" />
            <action android:name="com.google.android.libraries.cast.companionlibrary.action.stop" />
        </intent-filter>
    </receiver>
    <service
        android:name="com.google.android.libraries.cast.companionlibrary.notification.VideoCastNotificationService"
        android:exported="false" >
        <intent-filter>
            <action
                android:name="com.google.android.libraries.cast.companionlibrary.action.notificationvisibility" />
        </intent-filter>
    </service>
    <service
        android:name="com.google.android.libraries.cast.companionlibrary.cast.reconnection.ReconnectionService"/>
    <receiver android:name="com.google.android.gms.analytics.AnalyticsReceiver"
        android:enabled="true">
        <intent-filter>
            <action android:name="com.google.android.gms.analytics.ANALYTICS_DISPATCH" />
        </intent-filter>
    </receiver>

主要活动部分

public VideoCastManager mCastManager;    
@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);

//id changed for public use

    CastConfiguration options = new CastConfiguration.Builder("000000") 
.enableAutoReconnect()
.enableLockScreen()
.enableWifiReconnection()
.enableNotification()
.build();

    if(mCastManager == null) 
        VideoCastManager.initialize(this, options);
    
    mCastManager = VideoCastManager.getInstance();
//some other not related code here

我发现了完整的错误

04-07 22:12:32.185 5022-5022/? E/AndroidRuntime: FATAL EXCEPTION: main
                                             Process: lv.test.myapp, PID: 5022
                                             java.lang.RuntimeException: Unable to start receiver com.google.android.libraries.cast.companionlibrary.remotecontrol.VideoIntentReceiver: java.lang.IllegalStateException: No VideoCastManager instance was found, did you forget to initialize it?
                                                 at android.app.ActivityThread.handleReceiver(ActivityThread.java:3114)
                                                 at android.app.ActivityThread.access$1800(ActivityThread.java:181)
                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1551)
                                                 at android.os.Handler.dispatchMessage(Handler.java:102)
                                                 at android.os.Looper.loop(Looper.java:145)
                                                 at android.app.ActivityThread.main(ActivityThread.java:6117)
                                                 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:1399)
                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
                                              Caused by: java.lang.IllegalStateException: No VideoCastManager instance was found, did you forget to initialize it?
                                                 at com.google.android.libraries.cast.companionlibrary.cast.VideoCastManager.getInstance(VideoCastManager.java:259)
                                                 at com.google.android.libraries.cast.companionlibrary.remotecontrol.VideoIntentReceiver.onReceive(VideoIntentReceiver.java:49)
                                                 at android.app.ActivityThread.handleReceiver(ActivityThread.java:3107)
                                                 at android.app.ActivityThread.access$1800(ActivityThread.java:181) 
                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1551) 
                                                 at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                 at android.os.Looper.loop(Looper.java:145) 
                                                 at android.app.ActivityThread.main(ActivityThread.java:6117) 
                                                 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:1399) 
                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194) 
04-07 22:12:32.200 2888-3384/? V/ApplicationPolicy: isApplicationStateBlocked userId 0 pkgname lv.test.myapp

【问题讨论】:

【参考方案1】:

不知道您使用的是哪个版本的 CCL,但如果您使用的是最新版本,则应更新清单以从接收器块和服务中删除所有意图过滤器。对于接收器,看起来其他人正在尝试调用您的接收器,因此如果您删除意图过滤器,它将成为您的应用程序的私有,并且不会遇到此类问题。

【讨论】:

如果您使用自定义 VideoIntentReceiver 会怎样?

以上是关于VideoCastManager 在手机启动时崩溃 Android 应用程序的主要内容,如果未能解决你的问题,请参考以下文章

应用程序崩溃后更改手机语言并从后台重新启动

MySQL不启动后崩溃问题,怎么解决

在启动时触发BroadcastReceiver的应用程序崩溃

Android 9 (API 28) 活动在启动时崩溃 (setContentView)

如果应用程序崩溃一次,我无法启动应用程序,除非我重新启动我的 Android 手机

发布时的应用崩溃(材料主题问题)