简体   繁体   English

错误消息:您需要与此活动一起使用Theme.AppCompat主题

[英]Error Message: You need to use a Theme.AppCompat theme with this activity

This question has been asked so many times, but still i am unable to solve this problem. 这个问题已经被问过很多次了,但是我仍然无法解决这个问题。

I am trying to show Alert windows in my activity. 我正在尝试在活动中显示“警报”窗口。 Its giving me following error message 它给我以下错误消息

java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity java.lang.IllegalStateException:您需要在此活动中使用Theme.AppCompat主题(或后代)

This is my code. 这是我的代码。

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

                }
            }
        });

Manifest File 清单文件

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

    ></activity>

Style v21 风格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>

I have also changed the style of layout to AppComat.NoActionBar from Layout Design . 我还将布局样式from Layout Design更改为AppComat.NoActionBar。

Error Message 错误信息

 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)

I guess the error is caused because of using AlertDialog in your code. 我猜该错误是由于在您的代码中使用AlertDialog引起的。 You need to pass your activity's context instead of the application context to the alertdialog builder. 您需要将活动的上下文而不是应用程序上下文传递给Alertdialog构建器。 Get the context in your AlertDialog Builder using getActivity() instead of using getApplicationContext() . 使用getActivity()而不是getApplicationContext()AlertDialog Builder获取上下文。 Like this: 像这样:

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

Use the following theme in your style 在您的样式中使用以下主题

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

使用此代码

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

暂无
暂无

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

相关问题 错误:您需要在此活动中使用Theme.AppCompat主题(或后代) - Error: 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" 在 android 中具有自定义主题的应用程序,错误:您需要在此活动中使用 Theme.AppCompat 主题(或后代) - app with custom theme in android, error: You need to use a Theme.AppCompat theme (or descendant) with this activity 您需要使用Theme.AppCompat主题 - You need to use a Theme.AppCompat theme “您需要使用Theme.AppCompat主题” - “You need to use a Theme.AppCompat theme” 您需要使用Theme.AppCompat主题错误,即使主题设置为AppCompat也是如此 - You need to use a Theme.AppCompat theme error, even with theme set to AppCompat 错误:“您需要在此活动中使用 Theme.AppCompat 主题(或后代)。” 即使更改主题,go 也不会消失 - Error: " You need to use a Theme.AppCompat theme (or descendant) with this activity." wont go away even when changing theme java.lang.IllegalStateException:您需要在此活动中使用Theme.AppCompat主题(或后代) - java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity 无法启动MainActivity-您需要在此活动中使用Theme.AppCompat主题(或后代) - Unable to start MainActivity - 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
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM