错误消息:您需要在此活动中使用Theme.AppCompat主题

时间:2016-07-18 13:31:54

标签: java android android-layout android-studio

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

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

  

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.NoActionBar from 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)

3 个答案:

答案 0 :(得分:5)

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

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

答案 1 :(得分:1)

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

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

答案 2 :(得分:1)

使用此代码

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