BasicActivity 中的 AlertDialog 不起作用 Theme.AppCompat

我有一个 BasicActivity,我想在用户单击浮动操作按钮时显示一个 AlertDialog。当我在 FAB 上计时时:应用程序因以下错误而停止:


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

我试图从(默认 BasicActivity)更改我的活动主题:


android:theme="@style/AppTheme.NoActionBar"


android:theme="@style/Theme.AppCompat"

但是当我打开它并出现此错误时,整个活动都会停止:


This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.

我看到了很多关于这个错误的问题,但我仍然不明白。我只使用了一个默认活动 (BasicActivity),我想显示一个简单的 AlertDialog。


显现:


<application

    android:allowBackup="true"

    android:icon="@mipmap/ic_launcher"

    android:label="@string/app_name"

    android:roundIcon="@mipmap/ic_launcher_round"

    android:supportsRtl="true"

    android:theme="@style/AppTheme">

    <activity

        android:name=".MainActivity"

        android:label="@string/title_activity_main"

        android:theme="@style/AppTheme.NoActionBar">

        <intent-filter>

            <action android:name="android.intent.action.MAIN" />


            <category android:name="android.intent.category.LAUNCHER" />

        </intent-filter>

    </activity>

    <activity

        android:name=".ListActivity"

        android:label="@string/title_activity_list"

        android:launchMode="singleTop"

        android:theme="@style/AppTheme.NoActionBar"

        />

    <activity

        android:name=".AddActivity"

        android:label="@string/title_activity_add"

        android:theme="@style/AppTheme.NoActionBar">

    </activity>

    <activity

        android:name=".DetailsActivity"

        android:label="@string/title_activity_details"

        android:parentActivityName=".ListActivity"

        android:theme="@style/AppTheme.NoActionBar">

        <!-- Parent activity meta-data to support 4.0 and lower -->

        <meta-data

            android:name="android.support.PARENT_ACTIVITY"

            android:value=".MainActivity" />

    </activity>

</application>


胡说叔叔
浏览 197回答 2
2回答

长风秋雁

我得到了使用 [this] 关键字而不是 getApplicationContext() 的问题。当您使用 getApplicationContext() 时。它将应用应用程序的主题而不是活动。&nbsp;AlertDialog.Builder builder = new&nbsp;&nbsp;AlertDialog.Builder(this);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; builder.setIcon(android.R.drawable.ic_dialog_alert);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; builder.setTitle("MyTitle");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; builder.setMessage("MyMessage);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; builder.setCancelable(false);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; builder.setPositiveButton("SAVE", new DialogInterface.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onClick(DialogInterface dialog, int id) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (dialog != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dialog.dismiss();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });

慕桂英4014372

在清单中// 要显示的活动toolbar&nbsp; &nbsp; <activity android:name=".YourActivity"&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;android:theme="@style/AppTheme.NoActionBar"><!-- ADD THIS LINE -->在样式.xml<style name="AppTheme.NoActionBar">&nbsp; &nbsp; <item name="windowActionBar">false</item>&nbsp; &nbsp; <item name="windowNoTitle">true</item></style>将此添加到清单文件中的应用程序标记<application&nbsp; &nbsp; android:theme="@style/Theme.AppCompat.Light"</application>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java