Chromecast ExpandedControllerActivity 崩溃:尝试写入空对象引用上的字段 CastSeekBar

Chromecast ExpandedControllerActivity crashes: Attempt to write to field CastSeekBar on a null object reference

我在一个大型 Android 项目的团队中工作,该项目具有多种应用程序风格。我们有两种不同的 chromecast 实现,因为他的两种风格有不同的 UI 和要求。目前我正在尝试根据我的应用风格创建一个新的 ExpandedControllerActivity 以更符合标准实施。

但是我在运行时尝试打开扩展控制器时遇到了崩溃,而且我无法从日志消息中找出我做错了什么。

class MyAppChromecastExpandedControllerActivity : ExpandedControllerActivity() {
    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        super.onCreateOptionsMenu(menu)
        menuInflater.inflate(R.menu.actions, menu)
        CastButtonFactory.setUpMediaRouteButton(this, menu, R.id.media_route_menu_item)
        return true
    }
}

清单:

<activity
    android:name="dk.myapp.chromecast.MyAppChromecastExpandedControllerActivity"
    android:label="@string/app_name"
    android:launchMode="singleTask"
    android:theme="@style/MyAppCastControlsStyle"
    android:screenOrientation="portrait">
    <intent-filter>
        <action android:name="android.intent.action.MAIN"/>
    </intent-filter>
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value="dk.myapp.SynopsisActivity" />
</activity>

<meta-data
    android:name="com.google.android.gms.cast.framework.OPTIONS_PROVIDER_CLASS_NAME"
    android:value="dk.myapp.chromecast.CastOptionsProvider" />

风格:

<style name="MyAppCastControlsStyle" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="castExpandedControllerToolbarStyle">
        @style/ThemeOverlay.AppCompat.Dark.ActionBar
    </item>
    <item name="castControlButtons">@array/cast_expanded_controller_control_buttons</item>
</style>

<array name="cast_expanded_controller_control_buttons">
    <item>@id/cast_button_type_empty</item>
    <item>@id/cast_button_type_empty</item>
    <item>@id/cast_button_type_empty</item>
    <item>@id/cast_button_type_empty</item>
</array>

日志消息:

2021-05-31 10:40:06.651 1342-1342/dk.myapp.app.debug E/AndroidRuntime: FATAL EXCEPTION: main
Process: dk.myapp.app.debug, PID: 1342
java.lang.RuntimeException: Unable to start activity ComponentInfo{dk.myapp.app.debug/dk.myapp.chromecast.MyAppChromecastExpandedControllerActivity}: java.lang.NullPointerException: Attempt to write to field 'com.google.android.gms.cast.framework.media.widget.CastSeekBar$zza com.google.android.gms.cast.framework.media.widget.CastSeekBar.zzwt' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3114)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3257)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1948)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:214)
    at android.app.ActivityThread.main(ActivityThread.java:7050)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)
 Caused by: java.lang.NullPointerException: Attempt to write to field 'com.google.android.gms.cast.framework.media.widget.CastSeekBar$zza com.google.android.gms.cast.framework.media.widget.CastSeekBar.zzwt' on a null object reference
    at com.google.android.gms.cast.framework.media.uicontroller.UIMediaController.bindSeekBar(com.google.android.gms:play-services-cast-framework@@18.1.0:71)
    at com.google.android.gms.cast.framework.media.widget.ExpandedControllerActivity.onCreate(com.google.android.gms:play-services-cast-framework@@18.1.0:94)
    at android.app.Activity.performCreate(Activity.java:7327)
    at android.app.Activity.performCreate(Activity.java:7318)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3094)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3257) 
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1948) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loop(Looper.java:214) 
    at android.app.ActivityThread.main(ActivityThread.java:7050) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965) 

由于报错提到了我的MyAppChromecastExpandedControllerActivity,我估计是用了我的ExpandedControllerActivity,但后来我不明白为什么SeekBar会有问题。

折腾了一个星期,在Whosebug上发了个问题,然后半小时debug了问题

旧 ExpandedController 的自定义布局名为“cast_expanded_controller_activity”,与默认的 chromecast 布局同名。显然,自定义布局覆盖了默认布局,导致新的非自定义 ExpandedController 在布局上找不到搜索栏时崩溃。

因此,如果其他人遇到类似问题:请确保 none 您的控件或视图具有覆盖默认值的名称。