簡體   English   中英

Firebase java.lang.IllegalStateException:您需要在此活動中使用Theme.AppCompat主題(或后代)

[英]Firebase java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity

我讀過許多類似的問題,但不知道如何解決我的問題。

我在從Firebase接收雲消息通知的地方上了這節課:

public class FirebaseMessagingServiceImpl extends FirebaseMessagingService {

    private final static String TAG = "FirebaseMessaging";

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Log.i(TAG, "Message from " + remoteMessage.getFrom());
        if (remoteMessage.getNotification() != null) {
            final String content = remoteMessage.getNotification().getBody();
            final String title = remoteMessage.getNotification().getTitle();
            Log.i(TAG, "...with content: " + content);

            new Handler(Looper.getMainLooper()).post(new Runnable() {
                @Override
                public void run() {
                    UserInterfaceUtils.getAlertDialog(
                            getApplicationContext(),
                            title,
                            content,
                            android.R.string.ok,
                            android.R.string.no,
                            android.R.drawable.ic_dialog_alert,
                            null, null,
                            true, false).show();
                }
            });
        }
        super.onMessageReceived(remoteMessage);
    }

    @Override
    public void onDeletedMessages() {
        super.onDeletedMessages();
    }
}

從那里我要創建一個alert.dialog 但是我得到了這個IllegalStateException 當我記錄getApplicationContext()的字符串時,我得到的是: android.support.multidex.MultiDexApplication@42585d08

我很困在這里,不知道如何解決這個問題。

為了完整起見,我的getAlertDialog方法:

public static AlertDialog.Builder getAlertDialog(Context context, String title, String message, int pos, int neg, int icon, final Callable posFunc, final Callable negFunc, boolean posButton, boolean negButton) {
        AlertDialog.Builder builder;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            builder = new AlertDialog.Builder(context, R.style.ShopDialogTheme);
        } else {
            builder = new AlertDialog.Builder(context);
        }

        builder.setTitle(title)
                .setMessage(Html.fromHtml(message));

        if (posButton)
            builder.setPositiveButton(pos, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    try {
                        posFunc.call();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        if (negButton)
            builder.setNegativeButton(neg, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    try {
                        negFunc.call();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        builder.setCancelable(false);
        builder.setIcon(icon);
        return builder;
    }

根據標題,聽起來好像代碼運行時您擁有的任何Activity都在擴展Activity,並且它需要擴展AppCompatActivity,並且您在res / styles.xml中使用的任何主題都應該是AppCompat主題。

暫無
暫無

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

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