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

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


If you have another styles files in side another values folders like "values-v11", "values-v14"... Edit theme also and try to clean your app before running.

Edited:From your activity change getApplicationContext() to this:

AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());

to

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

Because the dialog also should extends the Appcompat Theme.


If anybody experiences this issue with activities, try to explicitly configure a theme to your activity.

<activity android:name=".activities.BLEControlActivity" android:theme="@style/Theme.AppCompat.DayNight"></activity>


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.