簡體   English   中英

使用 getFont 時出現 android.content.res.Resources$NotFoundException

[英]android.content.res.Resources$NotFoundException when using getFont

我開發了一個基於 Firebase 的 android 應用程序。

應用程序中的按鈕之一用於刪除帳戶。

刪除帳戶后,我使用以下命令將用戶返回到主要活動:

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( 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);

上面的代碼應該可以正常工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM