Compose FontFamily 错误:“必须初始化变量”

Ham*_*med 2 android android-jetpack-compose

我正在尝试将字体添加到我的应用程序中,但是当我在文件中声明字体时,Type.kt我收到此错误消息

变量“iranSansFontFamily”必须初始化

为什么我会收到此错误?

这是代码:

val CustomTypography = Typography(
    bodyLarge = TextStyle(
        fontFamily = iranSansFontFamily,
        fontWeight = FontWeight.Normal,
        fontSize = 16.sp,
        lineHeight = 24.sp,
        letterSpacing = 0.5.sp
    )
)

val iranSansFontFamily = FontFamily(
    Font(R.font.iransans_farsi_medium, FontWeight.Medium),
    Font(R.font.iransans_farsi_bold, FontWeight.Bold)
)
Run Code Online (Sandbox Code Playgroud)

Moh*_*jeb 5

字体系列的声明必须位于版式的顶部,如下所示:

val iranSansFontFamily = FontFamily(
    Font(R.font.iransans_farsi_medium, FontWeight.Medium),
    Font(R.font.iransans_farsi_bold, FontWeight.Bold)
)

val CustomTypography = Typography(
    bodyLarge = TextStyle(
        fontFamily = iranSansFontFamily,
        fontWeight = FontWeight.Normal,
        fontSize = 16.sp,
        lineHeight = 24.sp,
        letterSpacing = 0.5.sp
    )
)
Run Code Online (Sandbox Code Playgroud)