2

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!

Francesco Romano
  • 579
  • 1
  • 4
  • 12
  • Also Check [This thread](https://stackoverflow.com/questions/26515058/this-activity-already-has-an-action-bar-supplied-by-the-window-decor) just to make things clear . – ADM Jan 18 '19 at 12:01
  • Can't believe! The error was the line AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext()); – Francesco Romano Jan 18 '19 at 12:44

2 Answers2

4

I got the issue use [this] keyword instead of getApplicationContext(). when you use getApplicationContext(). It will apply the theme of app instead of activity.

 AlertDialog.Builder builder = new 
 AlertDialog.Builder(this);
            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();
                    }
                }
            });
Nitin Jain
  • 1,314
  • 11
  • 15
1

in manifest

// the activity where you want to show toolbar

    <activity android:name=".YourActivity"  
     android:theme="@style/AppTheme.NoActionBar"><!-- ADD THIS LINE -->

in styles.xml

<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

add this to your application tag in the Manifest file

<application
    android:theme="@style/Theme.AppCompat.Light"
</application>