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
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>
Try adding following to your proguard-rules:
-keep public class com.google.android.gms.**
-keep class android.support.v7.** { *; }
-keep interface android.support.v7.** { *; }
-dontwarn com.google.android.gms.**
If you are using
com.google.android.gms:play-services-ads:8.1.0
Have a look at Google Issue 190237
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.
in your style.xml file add below code-
style.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme.Base"
parent="@style/Theme.AppCompat.Light">
</style>
</resources>
also define theme like this in your activity
<activity
android:name=".DialogActivity"
android:label="@string/title_activity_test"
android:screenOrientation="portrait"
android:theme="@android:style/AppTheme.Dialog">
OR
<activity
android:name=".DialogActivity"
android:label="@string/title_activity_test"
android:screenOrientation="portrait"
android:theme="@style/AppTheme">
clean project and run again..
I face such a case and I was managed to solve it. Here it is:
public class MainActivity extends AppCompatActivity {
AlertDialog.Builder alertDialog;
alertDialog = new AlertDialog.Builder(this);
public void onFinish() {
alertDialog.setIcon(android.R.drawable.alert_light_frame);
alertDialog.setTitle("You are done");
alertDialog.setMessage("you got it");
alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
runTheCode();
}
});
alertDialog.show();
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.