android.content.res.Resources$NotFoundException 使用 getFont 时

问题描述

我开发了一个基于 Firebase 的安卓应用。

应用程序中的一个按钮用于删除帐户。

一旦帐户被删除,我将使用此将用户返回到主要活动:

AuthUI.getInstance()
        .signOut( context )
        .addOnCompleteListener( task -> {
            Intent intent = new Intent( context,MainActivity.class );
            intent.setFlags( Intent.FLAG_ACTIVITY_CLEAR_TOP |
                    Intent.FLAG_ACTIVITY_CLEAR_TASK |
                    Intent.FLAG_ACTIVITY_NEW_TASK );
            context.startActivity( intent );
        } );

那么,在创建主activity的时候,里面的方法就很少了。 当用户第一次打开应用程序告诉他们应该打开 GPS 时,会使用一种名为 activateGPS方法

private void activateGPS() {
    new GpsUtils( this ).turnGPSOn( isGPSEnable -> isGPS = isGPSEnable );

    if (!isGPS) {
        PopUps popUps = new PopUps();
        popUps.popSnack( getApplicationContext(),getwindow().getDecorView().findViewById( android.R.id.content ),getString( R.string.Location_Service ) );
    }
}

是什么导致了这个问题?

当我在模拟器上运行它时,一切似乎都正常。

谢谢 如果 GPS 关闭,则会显示小吃店。

问题是,我在日志中看到的是,当用户删除他们的帐户时,发生了以下错误

Caused by android.content.res.Resources$NotFoundException
Font resource ID #0x7f090000 Could not be retrieved.

androidx.core.content.res.ResourcesCompat.loadFont (ResourcesCompat.java:4)
androidx.core.content.res.ResourcesCompat.getFont (ResourcesCompat.java:19)
com.xx.yy.PopUps.popSnack (PopUps.java:4)
com.xx.yy.MainActivity.activateGPS (MainActivity.java:8)

PopUps.popSnack 构建如下,我从接口使用它:

public void popSnack(Context context,View view,String message) {
    Snackbar snackbar = Snackbar.make( view.findViewById( android.R.id.content ),message,Snackbar.LENGTH_SHORT );
    View sbView = snackbar.getView();
    sbView.setBackgroundColor( context.getResources().getColor( R.color.colorPrimaryDark ) );
    sbView.setElevation( 0f );

    TextView tv = (snackbar.getView()).findViewById( com.google.android.material.R.id.snackbar_text );
    Typeface font = ResourcesCompat.getFont( context,R.font.assistant );
    tv.setTypeface( font );
    tv.setTextSize( 12 );

    if (view.findViewById( R.id.et_Message ) != null) {
        snackbar.setAnchorView( R.id.et_Message );
    } else if (view.findViewById( R.id.bottom_navigation ) != null) {
        snackbar.setAnchorView( R.id.bottom_navigation );
    }
    snackbar.setDuration( 5000 );
    snackbar.show();
}

解决方法

根据您提到的错误,我认为您尝试访问的资源不可用。 请确保文件在字体文件夹中。

   ResourcesCompat.getFont(context,R.font.assistant);

上面的代码应该可以正常工作。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...