java.lang.IollegalStateException:您需要在Flutter应用程序中对此活动使用Theme



我在我的flutter项目中添加了支付网关sdk,但当我尝试在android上本地运行该项目时,应用程序崩溃并显示消息";java.lang.IollegalStateException:您需要将Theme.AppCompat主题(或子代(用于此活动"。如何解决此问题?

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.app">
<application
android:label="App"
android:icon="@mipmap/main_launcher"
android:usesCleartextTraffic="true">
<activity
android:name=".MainActivity"
android:launchMode="singleTop"
android:theme="@style/LaunchTheme"           android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize">
<meta-data
android:name="io.flutter.embedding.android.NormalTheme"
android:resource="@style/NormalTheme"
/>
<meta-data
android:name="io.flutter.embedding.android.SplashScreenDrawable"
android:resource="@drawable/launch_background"
/>
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value=""/>
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<activity
android:name="com.mastercard.gateway.android.sdk.Gateway3DSecureActivity"
android:label="@string/gateway_3d_secure_authentication" />
<meta-data
android:name="flutterEmbedding"
android:value="2" />
</application>
</manifest>

样式

<resources>
<style name="LaunchTheme" parent="@android:style/Theme.Light.NoTitleBar">
<item name="android:windowBackground">@drawable/launch_background</item>
</style>
<style name="NormalTheme" parent="@android:style/Theme.Light.NoTitleBar">
<item name="android:windowBackground">?android:colorBackground</item>
</style>
</resources>

从更改LaunchTheme

<style name="LaunchTheme" parent="@android:style/Theme.Light.NoTitleBar">

<style name="LaunchTheme" parent="Theme.AppCompat.Light.NoActionBar">

AppCompat主题现在被视为遗留主题,使用AndroidX您必须设置Material主题,如果不设置,您将在其他地方发生崩溃。因此,无论您选择了哪个主题,如果您的活动类扩展了AppCompatActivity,只需更改:

AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());

至:

AlertDialog.Builder builder = new AlertDialog.Builder(YourActivity.this);

如果您的活动类没有扩展AppCompatActivity,请改用DialogFragment

如果您使用的是Jetpack Compose,并且您的活动类扩展了ComponentActivity,请使用属于Compose Library的AlertDialog。(示例(

将其放入清单文件中的应用程序标记中

<application
android:label="tech5_ssnit_test"
android:name="${applicationName}"
android:theme="@style/Theme.AppCompat.Light" //This line
android:icon="@mipmap/ic_launcher">

相关内容