Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android getColor(…)返回-1_Android_Colors_Statusbar - Fatal编程技术网

Android getColor(…)返回-1

Android getColor(…)返回-1,android,colors,statusbar,Android,Colors,Statusbar,我试图通过当前显示片段的主题获取colorPrimartVariant颜色,并将状态栏颜色设置为该颜色。为此,我编写了下面的代码块 addOnDestinationChangedListener { controller, destination, arguments -> showBottomNavigationAndFabByDestinationId(destination.id) val fragment = hostFragment

我试图通过当前显示片段的主题获取colorPrimartVariant颜色,并将状态栏颜色设置为该颜色。为此,我编写了下面的代码块

addOnDestinationChangedListener { controller, destination, arguments ->
            showBottomNavigationAndFabByDestinationId(destination.id)
            val fragment = hostFragment?.childFragmentManager?.fragments?.first()
            fragment?.context?.let { fragmentContext ->
                val defaultColor = ResourcesCompat.getColor(resources,R.color.white,fragmentContext.theme)
                val color = MaterialColors.getColor(fragmentContext,R.attr.colorPrimaryVariant,defaultColor)
                Log.e("TAG", "addOnDestinationChangedListenerToAppContent: def: $defaultColor  color : $color", )
                window.statusBarColor = color

            }

        }
然而奇怪的是,ResourcesCompat.getColor(…)和MaterialColors.getColor(…)对每个片段都返回-1,这样状态栏的颜色就不会改变。 为什么会这样?有什么解决办法吗?谢谢