【问题标题】:Error Message: You need to use a Theme.AppCompat theme with this activity错误消息:您需要在此活动中使用 Theme.AppCompat 主题
【发布时间】:2016-11-21 03:23:00
【问题描述】:

这个问题已经被问了很多次了,但我仍然无法解决这个问题。

我正在尝试在我的活动中显示警报窗口。它给了我以下错误消息

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

这是我的代码。

public class CartActivity extends AppCompatActivity 

        btnCheckout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(cbApprove.isChecked()){

                    AlertDialog alertDialog = new AlertDialog.Builder(getApplicationContext()).create();
                    alertDialog.setTitle("Thanks");
                    alertDialog.setMessage("Thanks for participating in ticketing flow. The test ends here for the Beta");
                    alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Ok", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Intent i = new Intent(getApplicationContext(), MainActivity.class);
                            startActivity(i);
                        }
                    });

                    alertDialog.show();

                }
            }
        });

清单文件

<activity
    android:name=".ticketing.activities.CartActivity"
    android:label="@string/title_activity_cart"
    android:theme="@style/AppTheme"

    ></activity>

风格 v21

<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>
</style>

我也把布局样式改成了AppComat.NoActionBarfrom Layout Design

错误信息

 FATAL EXCEPTION: main
 Process: com.tixsee.mavs, PID: 15867
 java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
 at android.support.v7.app.AppCompatDelegateImplV7.createSubDecor(AppCompatDelegateImplV7.java:340)
 at android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:309)
 at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:273)
 at android.support.v7.app.AppCompatDialog.setContentView(AppCompatDialog.java:80)
 at android.support.v7.app.AlertController.installContent(AlertController.java:214)
 at android.support.v7.app.AlertDialog.onCreate(AlertDialog.java:256)
 at android.app.Dialog.dispatchOnCreate(Dialog.java:463)
 at android.app.Dialog.show(Dialog.java:288)
 at com.tixsee.mavs.ticketing.activities.CartActivity$1.onClick(CartActivity.java:92)
 at android.view.View.performClick(View.java:5156)
 at android.view.View$PerformClick.run(View.java:20755)
 at android.os.Handler.handleCallback(Handler.java:739)
 at android.os.Handler.dispatchMessage(Handler.java:95)
 at android.os.Looper.loop(Looper.java:145)
 at android.app.ActivityThread.main(ActivityThread.java:5832)
 at java.lang.reflect.Method.invoke(Native Method)
 at java.lang.reflect.Method.invoke(Method.java:372)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)

【问题讨论】:

  • 进入 mainfest 并更改活动主题
  • 请发布您的整个堆栈跟踪。
  • 你的哪个活动发生了错误变化,如 android:theme="@style/Theme.AppCompat"
  • 是的,它只适用于棒棒糖和更高版本,@Kishanpatel 我已经从布局设计更改了活动主题。
  • 访问此link 以解决您的问题。

标签: java android android-layout android-studio


【解决方案1】:

我猜这个错误是因为在你的代码中使用了 AlertDialog 造成的。您需要将活动的上下文而不是应用程序上下文传递给 alertdialog 构建器。使用getActivity() 而不是使用getApplicationContext() 获取AlertDialog Builder 中的上下文。像这样:

AlertDialog alertDialog = new AlertDialog.Builder(getActivity()).create();

【讨论】:

  • 谢谢,这解决了我的问题。我写了以下代码。 AlertDialog alertDialog = new AlertDialog.Builder(CartActivity.this).create();
【解决方案2】:

在您的风格中使用以下主题

<style name="MyAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
    <!-- Used for the buttons -->
    <item name="colorAccent">#FFC107</item>
    <!-- Used for the title and text -->
    <item name="android:textColorPrimary">#FFFFFF</item>
    <!-- Used for the background -->
    <item name="android:background">#4CAF50</item>
</style>

【讨论】:

    【解决方案3】:

    使用此代码

    AlertDialog alertDialog = new AlertDialog.Builder(this).create();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-27
      • 2014-03-15
      • 2015-11-27
      • 2016-07-04
      • 2016-05-16
      相关资源
      最近更新 更多