I have a BasicActivity and I want to show an AlertDialog when the user click on the Floating Action Button. When I clock on the FAB: the app stops with this error:
java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
I tried to change the Theme of my activty from (default BasicActivity):
android:theme="@style/AppTheme.NoActionBar"
to
android:theme="@style/Theme.AppCompat"
but the whole activity stops when I open it with this error:
This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
I saw many questions about this error but I still don't figure it out. I have used just a default activity (BasicActivity) and I want show a simple AlertDialog.
Manifest:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ListActivity"
android:label="@string/title_activity_list"
android:launchMode="singleTop"
android:theme="@style/AppTheme.NoActionBar"
/>
<activity
android:name=".AddActivity"
android:label="@string/title_activity_add"
android:theme="@style/AppTheme.NoActionBar">
</activity>
<activity
android:name=".DetailsActivity"
android:label="@string/title_activity_details"
android:parentActivityName=".ListActivity"
android:theme="@style/AppTheme.NoActionBar">
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
</application>
Activity:
public class ListActivity extends AppCompatActivity {
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
...
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
builder.setIcon(android.R.drawable.ic_dialog_alert);
builder.setTitle("MyTitle");
builder.setMessage("MyMessage);
builder.setCancelable(false);
builder.setPositiveButton("SAVE", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
if (dialog != null) {
dialog.dismiss();
}
}
});
builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
if (dialog != null) {
dialog.dismiss();
}
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
});
...
}
}
EDIT: The error wasn't related to AppTheme or other stuff, but just the line:
AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
must be replaced with:
AlertDialog.Builder builder = new AlertDialog.Builder(ListActivity.this);
I am really sorry for just wasting your time guys!