BasicActivity中的AlertDialog不起作用Theme.AppCompat

时间:2019-01-18 11:55:29

标签: java android alertdialog

我有一个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>

活动:

public class ListActivity extends AppCompatActivity {

    ...

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        ...

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
                builder.setIcon(android.R.drawable.ic_dialog_alert);
                builder.setTitle("MyTitle");
                builder.setMessage("MyMessage);
                builder.setCancelable(false);
                builder.setPositiveButton("SAVE", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        if (dialog != null) {
                            dialog.dismiss();
                        }
                    }
                });
                builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        if (dialog != null) {
                            dialog.dismiss();
                        }
                    }
                });
                AlertDialog alertDialog = builder.create();
                alertDialog.show();

            }
        });

        ...

    }

}

编辑:该错误与AppTheme或其他内容无关,而仅与以下内容有关:

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

必须替换为:

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

我很抱歉浪费您的时间!!

2 个答案:

答案 0 :(得分:2)

我使用[this]关键字而不是getApplicationContext()遇到问题。当您使用getApplicationContext()时。它将应用应用的主题而不是活动。

 AlertDialog.Builder builder = new 
 AlertDialog.Builder(this);
            builder.setIcon(android.R.drawable.ic_dialog_alert);
            builder.setTitle("MyTitle");
            builder.setMessage("MyMessage);
            builder.setCancelable(false);
            builder.setPositiveButton("SAVE", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    if (dialog != null) {
                        dialog.dismiss();
                    }
                }
            });

答案 1 :(得分:1)

清单中

//您要显示toolbar的活动

    <activity android:name=".YourActivity"  
     android:theme="@style/AppTheme.NoActionBar"><!-- ADD THIS LINE -->

在styles.xml中

<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

将其添加到清单文件中的应用程序标签中

<application
    android:theme="@style/Theme.AppCompat.Light"
</application>