Malik Malik - 1 month ago 6
Java Question

You need to use a Theme.AppCompat theme

Yes, I know, this problem was posted again and again here. But I can't find any solution to my specific problem.

Every time I try to make a Snackbar, my App crashes with


You need to use a Theme.AppCompat theme (or descendant) with the
design library.





Ok. I know: no debugging without the code, so:


The call, and line that throws the error:


Snackbar.make(
new CoordinatorLayout(getApplicationContext()),
"Logged out.",
Snackbar.LENGTH_SHORT).show();






The class definition:


public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {






Beginning of the manifest


<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".SplashActivity"
android:theme="@style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>

<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>





Oh yeah: The AppTheme and AppTheme.NoActionbar are both parents of Theme.AppCompat...




If you need anymore code, just tell me.

Answer

Why do you need CoordinatorLayout with getApplicationContext()?

You might even be able to make it work with new CoordinatorLayout(MainActivity.this). (this uses Activity Context instead of Application Context.

But you should really keep it simple. Try this:

Snackbar.make(findViewById(android.R.id.content), "Logged out.", Snackbar.LENGTH_SHORT).show();