提问者:小点点

IllegalStateException:您需要在此活动中使用theme.AppCompat主题(或后代)


我正试图在删除按钮上使用警报对话框。但是显示对话框时显示异常。当我单击delete时,它崩溃并在.Show上显示exception。

我试图使用theme.AppCompat主题进行此活动,但它仍然崩溃。

   <activity android:name=".AddEventActivity"
        android:theme="@style/Theme.AppCompat">
    </activity>


        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                new AlertDialog.Builder(getApplicationContext())
                        .setTitle("Delete entry")
                        .setMessage("Are you sure you want to delete this entry?")
                        .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {

                                Toast.makeText(getApplicationContext(), "Yaay", Toast.LENGTH_SHORT).show();
                                i = new Intent();
                                db.deleteEvent(eventData);
                                Log.i("d", "delete");
                                setResult(RESULT_OK, i);
                                finish();

                                // continue with delete
                            }
                        })
                        .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                // do nothing
                            }
                        })
                        .setIcon(android.R.drawable.ic_dialog_alert)
                        .show();
            }
        });
     <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>

        <item name="windowActionBarOverlay">false</item>

        <item name="windowActionBar">false</item>

        <item name="windowNoTitle">true</item>
        <item name="android:windowBackground">@color/background_material_light</item>
    </style>
    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

</resources>

共1个答案

匿名用户

你使用了错误的上下文,你必须使用活动上下文,而不是应用程序上下文

变化:

  new AlertDialog.Builder(getApplicationContext())

  new AlertDialog.Builder(YourActivityName.this)