当前位置: 首页 > 知识库问答 >
问题:

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

应向晨
2023-03-14

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

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

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

共有1个答案

易京
2023-03-14

我猜错误是由于在代码中使用了AlertDialog造成的。您需要将活动的上下文而不是应用程序上下文传递给alertdialog构建器。使用GetActivity()而不是使用GetApplicationContext()获取AlertDialog生成器中的上下文。像这样:

AlertDialog alertDialog = new AlertDialog.Builder(getActivity()).create();
 类似资料: