【问题标题】:java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activityjava.lang.IllegalStateException:您需要在此活动中使用 Theme.AppCompat 主题(或后代)
【发布时间】:2016-05-16 06:35:49
【问题描述】:

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

我尝试为此活动使用 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>

谢谢。

【问题讨论】:

  • 我认为异常是 BadTokenException。您需要关闭 NO 和 DELETE 按钮上的警报对话框
  • 按钮的onClick()?
  • setPositiveButton 和 setNegativeButton 的onClick

标签: java android android-appcompat android-theme illegalstateexception


【解决方案1】:

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

改变:

  new AlertDialog.Builder(getApplicationContext())

  new AlertDialog.Builder(YourActivityName.this)

【讨论】:

  • 在计划时间前 10 分钟设置您的 alarmManager
  • 并使用alarmManager.set方法
猜你喜欢
  • 2016-07-04
  • 2014-12-28
  • 2022-01-24
  • 2014-03-15
  • 1970-01-01
相关资源
最近更新 更多