How can I fix this error: You need to use a Theme.AppCompat theme (or descendant) with this activity

后端 未结 6 1562
醉梦人生
醉梦人生 2021-02-07 09:36

I searched all internet web sites to fix this error, but I couldn\'t. I just want to create AlertDialog with two button Yes and No.

This is my code:

impo         


        
6条回答
  •  醉话见心
    2021-02-07 10:08

    You are encountering this error because your AlertDialog is not using AppCompat theme while your activity is. To fix it, use the this property of your activity instead of getApplicationContext(), like so:

    AlertDialog.Builder builder = new AlertDialog.Builder(DialogActivity.this);
    

    The name of your activity, followed by .this, in this case DialogActivity.this, should always be used instead of only this, because if your dialog is created inside another class, for example an adapter of some sort, you will receive a compile-time error stating that a Context was expected instead of the adapter class given.

提交回复
热议问题