有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java 安卓。所容纳之物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 );
        } );

然后,当创建主活动时,其中几乎没有方法。 当用户第一次打开应用程序告诉他们应该打开GPS时,会使用一种称为activateGPS的方法

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

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

是什么导致了这个问题

当我在模拟器上运行它时,一切似乎都很好

多谢各位 如果GPS关闭,将显示一个快捷键

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

Caused by 安卓.content.res.Resources$NotFoundException
Font resource ID #0x7f090000 could not be retrieved.

安卓x.core.content.res.ResourcesCompat.loadFont (ResourcesCompat.java:4)
安卓x.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( 安卓.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.安卓.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();
}

共 (1) 个答案

  1. # 1 楼答案

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

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

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