简体   繁体   English

java.lang.IllegalStateException:此活动需要使用Theme.AppCompat主题(或后代)

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

I am trying to use alert dialog on delete button. 我试图在删除按钮上使用警报对话框。 But It is showing exception to show the dialog. 但是显示对话框显示异常。 When I click on delete it crashes and shows exception on .show. 当我单击删除时,它崩溃并在.show上显示异常。

I tried to use Theme.AppCompat theme for this activity but still it crashes. 我尝试将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();
            }
        });

My Theme 我的主题

     <!-- 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>

Thank you. 谢谢。

u are using the wrong context you have to use the activity context and not the application context 您使用了错误的上下文,您必须使用活动上下文而不是应用程序上下文

change: 更改:

  new AlertDialog.Builder(getApplicationContext())

to

  new AlertDialog.Builder(YourActivityName.this)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.IllegalStateException:您需要在此活动中使用Theme.AppCompat主题(或后代) - java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity java.lang.IllegalStateException您需要在此活动中使用Theme.AppCompat主题(或后代) - java.lang.IllegalStateException You need to use a Theme.AppCompat theme (or descendant) with this activity 如何解决:java.lang.IllegalStateException:您需要在此活动中使用 Theme.AppCompat 主题(或后代)? - How to resolve: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity? 定制主题android崩溃原因:java.lang.IllegalStateException:您需要在此活动中使用Theme.AppCompat主题(或后代) - Crash with custom theme android Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity 问题与您需要与此活动一起使用Theme.AppCompat主题(或后代)与该活动一起Theme.AppCompat主题(或后代)” - Issue with You need to use a Theme.AppCompat theme (or descendant) with this activity Theme.AppCompat theme (or descendant) with this activity" 如何解决:IllegalStateException:您需要在此活动中使用 Theme.AppCompat 主题(或后代) - How to solve: IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity Robolectric:IllegalStateException:您需要在此活动中使用 Theme.AppCompat 主题(或后代) - Robolectric: IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity 错误:您需要在此活动中使用Theme.AppCompat主题(或后代) - Error: You need to use a Theme.AppCompat theme (or descendant) with this activity java.lang.IllegalArgumentException:您需要将Theme.AppCompat主题(或后代)与设计库一起使用 - java.lang.IllegalArgumentException: You need to use a Theme.AppCompat theme (or descendant) with the design library 在 android 中具有自定义主题的应用程序,错误:您需要在此活动中使用 Theme.AppCompat 主题(或后代) - app with custom theme in android, error: You need to use a Theme.AppCompat theme (or descendant) with this activity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM