Android Crash on start: Styling the ActionBar Theme App.Compat - java

I don't want to post since it seems simple. For the sake of simplicity I'll provide as much detail as I can without throwing up a bunch of logcat and expecting a cure all.
Following the google tutorial for styling an action bar. Win7, Android Studio, Android 5, API 19 KitKat (Min SDK Version 11) no support library, Gradle 1.8 I think.
MainActivity.java excerpt:
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
//ERRORS: // super.onCreate(savedInstanceState);
//ERRORS: // setContentView(R.layout.activity_main);
getActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch (item.getItemId()) {
case R.id.action_search:
// openSearch();
return true;
case R.id.action_settings:
// openSettings();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
LogCat excerpt(when SuperNotCalled):
Process: gaga.june, PID: 8726
android.util.SuperNotCalledException: Activity {gaga.june/gaga.june.MainActivity} did not call through to super.onCreate()
LogCat excerpt (when I put in the Super):
Unable to start activity ComponentInfo{gaga.june/gaga.june.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
AndroidManifest.xml excerpt:
<uses-sdk android:minSdkVersion="11"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/CustomActionBarTheme"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:parentActivityName="gaga.june.MainActivity" >
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="gaga.june.MainActivity" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
WHAT I'VE TRIED:
Normally in the onCreate I would put super.onCreate(savedInstanceState);However, when I debug that line is the one that throws the error so some other people on SO commented it out to not inherit the previous overriden onCreate. I did the same
setContentView(R.layout.activity_main); Also gives me an error when debugging. Checked the manual, they said that you must define the ListView if it gives you an error like that. But I'm not using a listview, I only defined TextView Therefore, I commented it out
I changed my style from <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> to <style name="AppTheme" parent="Theme.AppCompat.Light">
Other than that I pretty much followed the tutorial exactly to my project. If I could just identify what all these problems mean that would be great (SO is always a last resort I may just skip this tutorial). Thanks so much
EDIT CustomActionBarTheme from themes.xml:
<!-- Theme applied to app/activity -->
<style name="CustomActionBarTheme"
parent="#android:style/Theme.Holo.Light">
<item name="android:actionBarStyle">#style/MyActionBar</item>
<item name="actionBarStyle">#style/MyActionBar</item>
</style>

First of all in your onCreate method:
you have to call the super method
you have to define your layout with setContentView method
you have to use the getSupportActionBar() instead of getActionBar method
Something like:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
Then you have to change the style CustomActionBarTheme used by your MainActivity, because the ActionBarActivity requires an AppCompat theme
<!-- Theme applied to app/activity -->
<style name="CustomActionBarTheme"
parent="Theme.AppCompat.Light">
......
</style>
Finally I suggest you switching to the new app-compat v 22.2.0 changing your build.gradle
dependencies {
compile 'com.android.support:appcompat-v7:22.2.0'
}
With this version the ActionBarActivity is deprecated. You can use now the AppCompatActivity

For Theme.AppCompat you need to include the support library in your gradle.build file:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.2.0'
}
Additionally ActionBarActivity is deprecated, use AppCompatActivity

The reason you are having this problem is because the activity you are trying
to apply the theme to is extending ActionBarActivity which
requires the AppCompat theme to be applied.
Change the parent of the actual java code to be just plain Activity and
you should be able to leave the theme on it.

Related

How to remove Header title in bottom nav bar - Android Studio

My objective is removing the orange bar thar has the label "App name".
Currently I have a top_app_bar and bottom_nav_bar. As far as I know the orange bar is generated by the bottom nav bar.
This is the bottom nav bar implementation in the activity_main.
This is the top app bar implementation in the activity_main.
The top bar is called ActionBar. The ActionBar is a part of the default layout of the theme you are using.
Method 1:
You can hide the ActionBar by creating a NO ActionBar Style in the Style.xml and setting the class's style to that.
In Style.xml
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
Then In the Manifest set the theme for the class to this NoActionBar Style.
In AndroidManifest.xml
<activity
android:name=".MainActivity"
android:theme="#style/AppTheme.NoActionBar" />
Method 2
Simply add a default NoActionBar Style in Manifest
<activity android:name=".MainActivity"
android:theme="#style/Theme.AppCompat.Light.NoActionBar" />
Method 3
You can do it programmatically
Java
if (getSupportActionBar() != null) {
getSupportActionBar().hide();
}
Kotlin
supportActionBar?.hide()
try
private void hide() {
// Hide UI first
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.hide();
}

App shuts down immediatly, but no mistakes in code shown [duplicate]

Android Studio 0.4.5
Android documentation for creating custom dialog boxes: http://developer.android.com/guide/topics/ui/dialogs.html
If you want a custom dialog, you can instead display an Activity as a dialog instead of using the Dialog APIs. Simply create an activity and set its theme to Theme.Holo.Dialog in
the <activity> manifest element:
<activity android:theme="#android:style/Theme.Holo.Dialog" >
However, when I tried this I get the following exception:
java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity
I am supporting the following, and I can't using something greater than 10 for the min:
minSdkVersion 10
targetSdkVersion 19
In my styles I have the following:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
And in my manifest I have this for the activity:
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:theme="#android:style/Theme.Holo.Light.Dialog"
android:name="com.ssd.register.Dialog_update"
android:label="#string/title_activity_dialog_update" >
</activity>
Creating the dialog box like this was something I was hopping to do, as I have already completed the layout.
Can anyone tell me how I can get around this problem?
The reason you are having this problem is because the activity you are trying to apply the dialog theme to is extending ActionBarActivity which requires the AppCompat theme to be applied.
Update: Extending AppCompatActivity would also have this problem
In this case, change the Java inheritance from ActionBarActivity to Activity and leave the dialog theme in the manifest as it is, a non Theme.AppCompat value
The general rule is that if you want your code to support older versions of Android, it should have the AppCompat theme and the java code should extend AppCompatActivity. If you have *an activity that doesn't need this support, such as you only care about the latest versions and features of Android, you can apply any theme to it but the java code must extend plain old Activity.
NOTE: When change from AppCompatActivity (or a subclass, ActionBarActivity), to Activity, must also change the various calls with "support" to the corresponding call without "support". So, instead of getSupportFragmentManager, call getFragmentManager.
All you need to do is add android:theme="#style/Theme.AppCompat.Light" to your application tag in the AndroidManifest.xml file.
Copying answer from #MarkKeen in the comments above as I had the same problem.
I had the error stated at the top of the post and happened after I added an alert dialog. I have all the relevant style information in the manifest. My problem was cured by changing a context reference in the alert builder - I changed:
new android.support.v7.app.AlertDialog.Builder(getApplicationContext())
to:
new android.support.v7.app.AlertDialog.Builder(this)
And no more problems.
If you are using the application context, like this:
final AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
change it to an activity context like this:
final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
min sdk is 10. ActionBar is available from api level 11. So for 10 you would be using AppCompat from the support library for which you need to use Theme.AppCompat or descendant of the same.
Use
android:theme="#style/Theme.AppCompat" >
Or if you dont want action bar at the top
android:theme="#style/Theme.AppCompat.NoActionBar">
More info #
http://developer.android.com/guide/topics/ui/actionbar.html
Edit:
I might have misread op post.
Seems op wants a Dialog with a Activity Theme. So as already suggested by Bobbake4 extend Activity instead of ActionBarActivity.
Also have a look # Dialog Attributes in the link below
http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.4_r1/frameworks/base/core/res/res/values/themes.xml/
I was experiencing this problem even though my Theme was an AppCompat Theme and my Activity was an AppCompatActivity (or Activity, as suggested on other's answers). So I cleaned, rebuild and rerun the project.
(Build -> Clean Project ; Build -> Rebuild Project ; Run -> Run)
It may seem dumb, but now it works great!
Just hope it helps!
This is what fixed it for me: instead of specifying the theme in manifest, I defined it in onCreate for each activity that extends ActionBarActivity:
#Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(R.style.MyAppTheme);
super.onCreate(savedInstanceState);
setContentView(R.layout.my_activity_layout);
...
}
Here MyAppTheme is a descendant of Theme.AppCompat, and is defined in xml. Note that the theme must be set before super.onCreate and setContentView.
go to your styles and put the parent
parent="Theme.AppCompat"
instead of
parent="#android:style/Theme.Holo.Light"
Change the theme of the desired Activity. This works for me:
<activity
android:name="HomeActivity"
android:screenOrientation="landscape"
android:theme="#style/Theme.AppCompat.Light"
android:windowSoftInputMode="stateHidden" />
In my case i have no values-v21 file in my res directory. Then i created it and added in it following codes:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
Just Do
new AlertDialog.Builder(this)
Instead of
new AlertDialog.Builder(getApplicationContext())
I had such crash on Samsung devices even though the activity did use Theme.AppCompat.
The root cause was related to weird optimizations on Samsung side:
- if one activity of your app has theme not inherited from Theme.AppCompat
- and it has also `android:launchMode="singleTask"`
- then all the activities that are launched from it will share the same Theme
My solution was just removing android:launchMode="singleTask"
If you need to extend ActionBarActivity you need on your style.xml:
<!-- Base application theme. -->
<style name="AppTheme" parent="AppTheme.Base"/>
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
If you set as main theme of your application as android:Theme.Material.Light instead of AppTheme.Base then you’ll get an “IllegalStateException:You need to use a Theme.AppCompat theme (or descendant) with this activity” error.
I had the same problem, but it solved when i put this on manifest: android:theme="#style/Theme.AppCompat.
<application
android:allowBackup="true"
android:icon="#drawable/icon"
android:label="#string/app_name_test"
android:supportsRtl="true"
android:theme="#style/Theme.AppCompat">
...
</application>
for me a solution, after trying all solutions from here, was to change
<activity
android:name="com.github.cythara.MainActivity"
android:label="Main">
</activity>
to include a theme:
<activity
android:name="com.github.cythara.MainActivity"
android:theme="#style/Theme.AppCompat.NoActionBar"
android:label="Main">
</activity>
In my case such issue was appear when i tried to show Dialog.
The problem was in context, I've use getBaseContext() which theoretically should return Activity context, but appears its not, or it return context before any Theme applied.
So I just replaced getBaseContexts() with "this", and now it work as expected.
Dialog.showAlert(this, title, message,....);
You have came to this because you want to apply Material Design in your theme style in previous sdk versions to 21. ActionBarActivity requires AppThemeso if you also want to prevent your own customization about your AppTheme, only you have to change in your styles.xml (previous to sdk 21) so this way, can inherit for an App Compat theme.
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
for this:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
I had an activity with theme <android:theme="#android:style/Theme.Dialog"> used for showing dialog in my appWidget and i had same problem
i solved this error by changing activity code like below:
#Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(R.style.Theme_AppCompat_Dialog); //this line i added
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dialog);
}
Make sure you are using an activity context while creating a new Alert Dialog and not an application or base context.
for me was solution to use ContextThemeWrapper:
private FloatingActionButton getFAB() {
Context context = new android.support.v7.view.ContextThemeWrapper(getContext(), R.style.AppTheme);
FloatingActionButton fab = new FloatingActionButton(context);
return fab;}
from Android - How to create FAB programmatically?
I had this problem as well and what I did to fix it, AND still use the Holo theme was to take these steps:
first I replaced this import:
import android.support.v7.app.AppCompatActivity;
with this one:
import android.app.Activity;
then changed my extension from:
public class MyClass extends AppCompatActivity {//...
to this:
public class MyClass extends Activity {//...
And also had to change this import:
import android.support.v7.app.AlertDialog;
to this import:
import android.app.AlertDialog;
and then you can use your theme tag in the manifest at the activity level:
android:theme="#android:style/Theme.Holo.Dialog" />
and lastly, (unless you have other classes in your project that has to use v7 appCompat) you can either clean and rebuild your project or delete this entry in the gradle build file at the app level:
compile 'com.android.support:appcompat-v7:23.2.1'
if you have other classes in your project that has to use v7 appCompat then just clean and rebuild the project.
In case the AndroidX SplashScreen library brought you here ...
This is because Theme.SplashScreen also has no R.styleable.AppCompatTheme_windowActionBar:
if (!a.hasValue(R.styleable.AppCompatTheme_windowActionBar)) {
a.recycle();
throw new IllegalStateException(
"You need to use a Theme.AppCompat theme (or descendant) with this activity.");
}
This requires switching the theme to the postSplashScreenTheme, before calling super():
#Override
protected void onCreate(Bundle savedInstanceState) {
/* When switching the theme to dark mode. */
if (savedInstanceState != null) {
this.setTheme(R.style.AppTheme);
}
super.onCreate(savedInstanceState);
/* When starting the Activity. */
if (savedInstanceState == null) {
SplashScreen.installSplashScreen(this);
}
}
Then the Theme.SplashScreen from AndroidManifest.xml won't interfere.
Also quite related: When using Theme.MaterialComponents, there's a bridge theme contained, which works as substitute for Theme.AppCompat: Theme.MaterialComponents.DayNight.NoActionBar.Bridge.
This Bridge theme works despite Theme.MaterialComponents not inherits from Theme.AppCompat:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar.Bridge" />
<style name="AppTheme.SplashScreen" parent="Theme.SplashScreen" />
</resources>
You have many solutions to that error.
You should use Activity or FragmentActivity instead of ActionbarActivity or AppCompatActivity
If you want use ActionbarActivity or AppCompatActivity, you should change in styles.xml Theme.Holo.xxxx to Theme.AppCompat.Light (if necessary add to DarkActionbar)
If you don't need advanced attributes about action bar or AppCompat you don't need to use Actionbar or AppCompat.
In Android manifest just change theme of activity to AppTheme as follow code snippet
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme">
</activity>
In my experiences the problem was the context where I showed my dialog.
Inside a button click I instantiate an AlertDialog in this way:
builder = new AlertDialog.Builder(getApplicationContext());
But the context was not correct and caused the error. I've changed it using the application context in this way:
In declare section:
Context mContext;
in the onCreate method
mContext = this;
And finally in the code where I need the AlertDialog:
start_stop = (Button) findViewById(R.id.start_stop);
start_stop.setOnClickListener( new View.OnClickListener()
{
#Override
public void onClick(View v)
{
if (!is_running)
{
builder = new AlertDialog.Builder(mContext);
builder.setMessage("MYTEXT")
.setCancelable(false)
.setPositiveButton("SI", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Task_Started = false;
startTask();
}
})
.setNegativeButton("NO",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
}
This is the solution for me.
I was getting this same problem. Because i was creating custom navigation drawer. But i forget to mention theme in my manifest like this
android:theme="#style/Theme.AppCompat.NoActionBar"
As soon i added the above the theme to my manifest it resolved the problem.
I have faced same problem.
If you are providing context to any class or method then provide YourActivityName.this instead of getApplicationContext().
Do this
builder = new AlertDialog.Builder(YourActivity.this);
Instead of
builder = new AlertDialog.Builder(getApplicationContext());
Change your theme style parent to
parent="Theme.AppCompat"
This worked for me ...
This one worked for me:
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/Theme.AppCompat.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Your Activity is extending ActionBarActivity which requires the AppCompat.theme to be applied.
Change from ActionBarActivity to Activity or FragmentActivity, it will solve the problem.
If you use no Action bar then :
android:theme="#android:style/Theme.Light.NoTitleBar.Fullscreen"

Error:-java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity [duplicate]

Android Studio 0.4.5
Android documentation for creating custom dialog boxes: http://developer.android.com/guide/topics/ui/dialogs.html
If you want a custom dialog, you can instead display an Activity as a dialog instead of using the Dialog APIs. Simply create an activity and set its theme to Theme.Holo.Dialog in
the <activity> manifest element:
<activity android:theme="#android:style/Theme.Holo.Dialog" >
However, when I tried this I get the following exception:
java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity
I am supporting the following, and I can't using something greater than 10 for the min:
minSdkVersion 10
targetSdkVersion 19
In my styles I have the following:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
And in my manifest I have this for the activity:
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:theme="#android:style/Theme.Holo.Light.Dialog"
android:name="com.ssd.register.Dialog_update"
android:label="#string/title_activity_dialog_update" >
</activity>
Creating the dialog box like this was something I was hopping to do, as I have already completed the layout.
Can anyone tell me how I can get around this problem?
The reason you are having this problem is because the activity you are trying to apply the dialog theme to is extending ActionBarActivity which requires the AppCompat theme to be applied.
Update: Extending AppCompatActivity would also have this problem
In this case, change the Java inheritance from ActionBarActivity to Activity and leave the dialog theme in the manifest as it is, a non Theme.AppCompat value
The general rule is that if you want your code to support older versions of Android, it should have the AppCompat theme and the java code should extend AppCompatActivity. If you have *an activity that doesn't need this support, such as you only care about the latest versions and features of Android, you can apply any theme to it but the java code must extend plain old Activity.
NOTE: When change from AppCompatActivity (or a subclass, ActionBarActivity), to Activity, must also change the various calls with "support" to the corresponding call without "support". So, instead of getSupportFragmentManager, call getFragmentManager.
All you need to do is add android:theme="#style/Theme.AppCompat.Light" to your application tag in the AndroidManifest.xml file.
Copying answer from #MarkKeen in the comments above as I had the same problem.
I had the error stated at the top of the post and happened after I added an alert dialog. I have all the relevant style information in the manifest. My problem was cured by changing a context reference in the alert builder - I changed:
new android.support.v7.app.AlertDialog.Builder(getApplicationContext())
to:
new android.support.v7.app.AlertDialog.Builder(this)
And no more problems.
If you are using the application context, like this:
final AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
change it to an activity context like this:
final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
min sdk is 10. ActionBar is available from api level 11. So for 10 you would be using AppCompat from the support library for which you need to use Theme.AppCompat or descendant of the same.
Use
android:theme="#style/Theme.AppCompat" >
Or if you dont want action bar at the top
android:theme="#style/Theme.AppCompat.NoActionBar">
More info #
http://developer.android.com/guide/topics/ui/actionbar.html
Edit:
I might have misread op post.
Seems op wants a Dialog with a Activity Theme. So as already suggested by Bobbake4 extend Activity instead of ActionBarActivity.
Also have a look # Dialog Attributes in the link below
http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.4_r1/frameworks/base/core/res/res/values/themes.xml/
I was experiencing this problem even though my Theme was an AppCompat Theme and my Activity was an AppCompatActivity (or Activity, as suggested on other's answers). So I cleaned, rebuild and rerun the project.
(Build -> Clean Project ; Build -> Rebuild Project ; Run -> Run)
It may seem dumb, but now it works great!
Just hope it helps!
This is what fixed it for me: instead of specifying the theme in manifest, I defined it in onCreate for each activity that extends ActionBarActivity:
#Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(R.style.MyAppTheme);
super.onCreate(savedInstanceState);
setContentView(R.layout.my_activity_layout);
...
}
Here MyAppTheme is a descendant of Theme.AppCompat, and is defined in xml. Note that the theme must be set before super.onCreate and setContentView.
go to your styles and put the parent
parent="Theme.AppCompat"
instead of
parent="#android:style/Theme.Holo.Light"
Change the theme of the desired Activity. This works for me:
<activity
android:name="HomeActivity"
android:screenOrientation="landscape"
android:theme="#style/Theme.AppCompat.Light"
android:windowSoftInputMode="stateHidden" />
In my case i have no values-v21 file in my res directory. Then i created it and added in it following codes:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
Just Do
new AlertDialog.Builder(this)
Instead of
new AlertDialog.Builder(getApplicationContext())
I had such crash on Samsung devices even though the activity did use Theme.AppCompat.
The root cause was related to weird optimizations on Samsung side:
- if one activity of your app has theme not inherited from Theme.AppCompat
- and it has also `android:launchMode="singleTask"`
- then all the activities that are launched from it will share the same Theme
My solution was just removing android:launchMode="singleTask"
If you need to extend ActionBarActivity you need on your style.xml:
<!-- Base application theme. -->
<style name="AppTheme" parent="AppTheme.Base"/>
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
If you set as main theme of your application as android:Theme.Material.Light instead of AppTheme.Base then you’ll get an “IllegalStateException:You need to use a Theme.AppCompat theme (or descendant) with this activity” error.
I had the same problem, but it solved when i put this on manifest: android:theme="#style/Theme.AppCompat.
<application
android:allowBackup="true"
android:icon="#drawable/icon"
android:label="#string/app_name_test"
android:supportsRtl="true"
android:theme="#style/Theme.AppCompat">
...
</application>
for me a solution, after trying all solutions from here, was to change
<activity
android:name="com.github.cythara.MainActivity"
android:label="Main">
</activity>
to include a theme:
<activity
android:name="com.github.cythara.MainActivity"
android:theme="#style/Theme.AppCompat.NoActionBar"
android:label="Main">
</activity>
In my case such issue was appear when i tried to show Dialog.
The problem was in context, I've use getBaseContext() which theoretically should return Activity context, but appears its not, or it return context before any Theme applied.
So I just replaced getBaseContexts() with "this", and now it work as expected.
Dialog.showAlert(this, title, message,....);
You have came to this because you want to apply Material Design in your theme style in previous sdk versions to 21. ActionBarActivity requires AppThemeso if you also want to prevent your own customization about your AppTheme, only you have to change in your styles.xml (previous to sdk 21) so this way, can inherit for an App Compat theme.
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
for this:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
I had an activity with theme <android:theme="#android:style/Theme.Dialog"> used for showing dialog in my appWidget and i had same problem
i solved this error by changing activity code like below:
#Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(R.style.Theme_AppCompat_Dialog); //this line i added
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dialog);
}
Make sure you are using an activity context while creating a new Alert Dialog and not an application or base context.
for me was solution to use ContextThemeWrapper:
private FloatingActionButton getFAB() {
Context context = new android.support.v7.view.ContextThemeWrapper(getContext(), R.style.AppTheme);
FloatingActionButton fab = new FloatingActionButton(context);
return fab;}
from Android - How to create FAB programmatically?
I had this problem as well and what I did to fix it, AND still use the Holo theme was to take these steps:
first I replaced this import:
import android.support.v7.app.AppCompatActivity;
with this one:
import android.app.Activity;
then changed my extension from:
public class MyClass extends AppCompatActivity {//...
to this:
public class MyClass extends Activity {//...
And also had to change this import:
import android.support.v7.app.AlertDialog;
to this import:
import android.app.AlertDialog;
and then you can use your theme tag in the manifest at the activity level:
android:theme="#android:style/Theme.Holo.Dialog" />
and lastly, (unless you have other classes in your project that has to use v7 appCompat) you can either clean and rebuild your project or delete this entry in the gradle build file at the app level:
compile 'com.android.support:appcompat-v7:23.2.1'
if you have other classes in your project that has to use v7 appCompat then just clean and rebuild the project.
In case the AndroidX SplashScreen library brought you here ...
This is because Theme.SplashScreen also has no R.styleable.AppCompatTheme_windowActionBar:
if (!a.hasValue(R.styleable.AppCompatTheme_windowActionBar)) {
a.recycle();
throw new IllegalStateException(
"You need to use a Theme.AppCompat theme (or descendant) with this activity.");
}
This requires switching the theme to the postSplashScreenTheme, before calling super():
#Override
protected void onCreate(Bundle savedInstanceState) {
/* When switching the theme to dark mode. */
if (savedInstanceState != null) {
this.setTheme(R.style.AppTheme);
}
super.onCreate(savedInstanceState);
/* When starting the Activity. */
if (savedInstanceState == null) {
SplashScreen.installSplashScreen(this);
}
}
Then the Theme.SplashScreen from AndroidManifest.xml won't interfere.
Also quite related: When using Theme.MaterialComponents, there's a bridge theme contained, which works as substitute for Theme.AppCompat: Theme.MaterialComponents.DayNight.NoActionBar.Bridge.
This Bridge theme works despite Theme.MaterialComponents not inherits from Theme.AppCompat:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar.Bridge" />
<style name="AppTheme.SplashScreen" parent="Theme.SplashScreen" />
</resources>
You have many solutions to that error.
You should use Activity or FragmentActivity instead of ActionbarActivity or AppCompatActivity
If you want use ActionbarActivity or AppCompatActivity, you should change in styles.xml Theme.Holo.xxxx to Theme.AppCompat.Light (if necessary add to DarkActionbar)
If you don't need advanced attributes about action bar or AppCompat you don't need to use Actionbar or AppCompat.
In Android manifest just change theme of activity to AppTheme as follow code snippet
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme">
</activity>
In my experiences the problem was the context where I showed my dialog.
Inside a button click I instantiate an AlertDialog in this way:
builder = new AlertDialog.Builder(getApplicationContext());
But the context was not correct and caused the error. I've changed it using the application context in this way:
In declare section:
Context mContext;
in the onCreate method
mContext = this;
And finally in the code where I need the AlertDialog:
start_stop = (Button) findViewById(R.id.start_stop);
start_stop.setOnClickListener( new View.OnClickListener()
{
#Override
public void onClick(View v)
{
if (!is_running)
{
builder = new AlertDialog.Builder(mContext);
builder.setMessage("MYTEXT")
.setCancelable(false)
.setPositiveButton("SI", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Task_Started = false;
startTask();
}
})
.setNegativeButton("NO",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
}
This is the solution for me.
I was getting this same problem. Because i was creating custom navigation drawer. But i forget to mention theme in my manifest like this
android:theme="#style/Theme.AppCompat.NoActionBar"
As soon i added the above the theme to my manifest it resolved the problem.
I have faced same problem.
If you are providing context to any class or method then provide YourActivityName.this instead of getApplicationContext().
Do this
builder = new AlertDialog.Builder(YourActivity.this);
Instead of
builder = new AlertDialog.Builder(getApplicationContext());
Change your theme style parent to
parent="Theme.AppCompat"
This worked for me ...
This one worked for me:
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/Theme.AppCompat.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Your Activity is extending ActionBarActivity which requires the AppCompat.theme to be applied.
Change from ActionBarActivity to Activity or FragmentActivity, it will solve the problem.
If you use no Action bar then :
android:theme="#android:style/Theme.Light.NoTitleBar.Fullscreen"

API LEVEL 21: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity [duplicate]

Android Studio 0.4.5
Android documentation for creating custom dialog boxes: http://developer.android.com/guide/topics/ui/dialogs.html
If you want a custom dialog, you can instead display an Activity as a dialog instead of using the Dialog APIs. Simply create an activity and set its theme to Theme.Holo.Dialog in
the <activity> manifest element:
<activity android:theme="#android:style/Theme.Holo.Dialog" >
However, when I tried this I get the following exception:
java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity
I am supporting the following, and I can't using something greater than 10 for the min:
minSdkVersion 10
targetSdkVersion 19
In my styles I have the following:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
And in my manifest I have this for the activity:
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:theme="#android:style/Theme.Holo.Light.Dialog"
android:name="com.ssd.register.Dialog_update"
android:label="#string/title_activity_dialog_update" >
</activity>
Creating the dialog box like this was something I was hopping to do, as I have already completed the layout.
Can anyone tell me how I can get around this problem?
The reason you are having this problem is because the activity you are trying to apply the dialog theme to is extending ActionBarActivity which requires the AppCompat theme to be applied.
Update: Extending AppCompatActivity would also have this problem
In this case, change the Java inheritance from ActionBarActivity to Activity and leave the dialog theme in the manifest as it is, a non Theme.AppCompat value
The general rule is that if you want your code to support older versions of Android, it should have the AppCompat theme and the java code should extend AppCompatActivity. If you have *an activity that doesn't need this support, such as you only care about the latest versions and features of Android, you can apply any theme to it but the java code must extend plain old Activity.
NOTE: When change from AppCompatActivity (or a subclass, ActionBarActivity), to Activity, must also change the various calls with "support" to the corresponding call without "support". So, instead of getSupportFragmentManager, call getFragmentManager.
All you need to do is add android:theme="#style/Theme.AppCompat.Light" to your application tag in the AndroidManifest.xml file.
Copying answer from #MarkKeen in the comments above as I had the same problem.
I had the error stated at the top of the post and happened after I added an alert dialog. I have all the relevant style information in the manifest. My problem was cured by changing a context reference in the alert builder - I changed:
new android.support.v7.app.AlertDialog.Builder(getApplicationContext())
to:
new android.support.v7.app.AlertDialog.Builder(this)
And no more problems.
If you are using the application context, like this:
final AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
change it to an activity context like this:
final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
min sdk is 10. ActionBar is available from api level 11. So for 10 you would be using AppCompat from the support library for which you need to use Theme.AppCompat or descendant of the same.
Use
android:theme="#style/Theme.AppCompat" >
Or if you dont want action bar at the top
android:theme="#style/Theme.AppCompat.NoActionBar">
More info #
http://developer.android.com/guide/topics/ui/actionbar.html
Edit:
I might have misread op post.
Seems op wants a Dialog with a Activity Theme. So as already suggested by Bobbake4 extend Activity instead of ActionBarActivity.
Also have a look # Dialog Attributes in the link below
http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.4_r1/frameworks/base/core/res/res/values/themes.xml/
I was experiencing this problem even though my Theme was an AppCompat Theme and my Activity was an AppCompatActivity (or Activity, as suggested on other's answers). So I cleaned, rebuild and rerun the project.
(Build -> Clean Project ; Build -> Rebuild Project ; Run -> Run)
It may seem dumb, but now it works great!
Just hope it helps!
This is what fixed it for me: instead of specifying the theme in manifest, I defined it in onCreate for each activity that extends ActionBarActivity:
#Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(R.style.MyAppTheme);
super.onCreate(savedInstanceState);
setContentView(R.layout.my_activity_layout);
...
}
Here MyAppTheme is a descendant of Theme.AppCompat, and is defined in xml. Note that the theme must be set before super.onCreate and setContentView.
go to your styles and put the parent
parent="Theme.AppCompat"
instead of
parent="#android:style/Theme.Holo.Light"
Change the theme of the desired Activity. This works for me:
<activity
android:name="HomeActivity"
android:screenOrientation="landscape"
android:theme="#style/Theme.AppCompat.Light"
android:windowSoftInputMode="stateHidden" />
In my case i have no values-v21 file in my res directory. Then i created it and added in it following codes:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
Just Do
new AlertDialog.Builder(this)
Instead of
new AlertDialog.Builder(getApplicationContext())
I had such crash on Samsung devices even though the activity did use Theme.AppCompat.
The root cause was related to weird optimizations on Samsung side:
- if one activity of your app has theme not inherited from Theme.AppCompat
- and it has also `android:launchMode="singleTask"`
- then all the activities that are launched from it will share the same Theme
My solution was just removing android:launchMode="singleTask"
If you need to extend ActionBarActivity you need on your style.xml:
<!-- Base application theme. -->
<style name="AppTheme" parent="AppTheme.Base"/>
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
If you set as main theme of your application as android:Theme.Material.Light instead of AppTheme.Base then you’ll get an “IllegalStateException:You need to use a Theme.AppCompat theme (or descendant) with this activity” error.
I had the same problem, but it solved when i put this on manifest: android:theme="#style/Theme.AppCompat.
<application
android:allowBackup="true"
android:icon="#drawable/icon"
android:label="#string/app_name_test"
android:supportsRtl="true"
android:theme="#style/Theme.AppCompat">
...
</application>
for me a solution, after trying all solutions from here, was to change
<activity
android:name="com.github.cythara.MainActivity"
android:label="Main">
</activity>
to include a theme:
<activity
android:name="com.github.cythara.MainActivity"
android:theme="#style/Theme.AppCompat.NoActionBar"
android:label="Main">
</activity>
In my case such issue was appear when i tried to show Dialog.
The problem was in context, I've use getBaseContext() which theoretically should return Activity context, but appears its not, or it return context before any Theme applied.
So I just replaced getBaseContexts() with "this", and now it work as expected.
Dialog.showAlert(this, title, message,....);
You have came to this because you want to apply Material Design in your theme style in previous sdk versions to 21. ActionBarActivity requires AppThemeso if you also want to prevent your own customization about your AppTheme, only you have to change in your styles.xml (previous to sdk 21) so this way, can inherit for an App Compat theme.
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
for this:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
I had an activity with theme <android:theme="#android:style/Theme.Dialog"> used for showing dialog in my appWidget and i had same problem
i solved this error by changing activity code like below:
#Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(R.style.Theme_AppCompat_Dialog); //this line i added
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dialog);
}
Make sure you are using an activity context while creating a new Alert Dialog and not an application or base context.
for me was solution to use ContextThemeWrapper:
private FloatingActionButton getFAB() {
Context context = new android.support.v7.view.ContextThemeWrapper(getContext(), R.style.AppTheme);
FloatingActionButton fab = new FloatingActionButton(context);
return fab;}
from Android - How to create FAB programmatically?
I had this problem as well and what I did to fix it, AND still use the Holo theme was to take these steps:
first I replaced this import:
import android.support.v7.app.AppCompatActivity;
with this one:
import android.app.Activity;
then changed my extension from:
public class MyClass extends AppCompatActivity {//...
to this:
public class MyClass extends Activity {//...
And also had to change this import:
import android.support.v7.app.AlertDialog;
to this import:
import android.app.AlertDialog;
and then you can use your theme tag in the manifest at the activity level:
android:theme="#android:style/Theme.Holo.Dialog" />
and lastly, (unless you have other classes in your project that has to use v7 appCompat) you can either clean and rebuild your project or delete this entry in the gradle build file at the app level:
compile 'com.android.support:appcompat-v7:23.2.1'
if you have other classes in your project that has to use v7 appCompat then just clean and rebuild the project.
In case the AndroidX SplashScreen library brought you here ...
This is because Theme.SplashScreen also has no R.styleable.AppCompatTheme_windowActionBar:
if (!a.hasValue(R.styleable.AppCompatTheme_windowActionBar)) {
a.recycle();
throw new IllegalStateException(
"You need to use a Theme.AppCompat theme (or descendant) with this activity.");
}
This requires switching the theme to the postSplashScreenTheme, before calling super():
#Override
protected void onCreate(Bundle savedInstanceState) {
/* When switching the theme to dark mode. */
if (savedInstanceState != null) {
this.setTheme(R.style.AppTheme);
}
super.onCreate(savedInstanceState);
/* When starting the Activity. */
if (savedInstanceState == null) {
SplashScreen.installSplashScreen(this);
}
}
Then the Theme.SplashScreen from AndroidManifest.xml won't interfere.
Also quite related: When using Theme.MaterialComponents, there's a bridge theme contained, which works as substitute for Theme.AppCompat: Theme.MaterialComponents.DayNight.NoActionBar.Bridge.
This Bridge theme works despite Theme.MaterialComponents not inherits from Theme.AppCompat:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar.Bridge" />
<style name="AppTheme.SplashScreen" parent="Theme.SplashScreen" />
</resources>
You have many solutions to that error.
You should use Activity or FragmentActivity instead of ActionbarActivity or AppCompatActivity
If you want use ActionbarActivity or AppCompatActivity, you should change in styles.xml Theme.Holo.xxxx to Theme.AppCompat.Light (if necessary add to DarkActionbar)
If you don't need advanced attributes about action bar or AppCompat you don't need to use Actionbar or AppCompat.
In Android manifest just change theme of activity to AppTheme as follow code snippet
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme">
</activity>
In my experiences the problem was the context where I showed my dialog.
Inside a button click I instantiate an AlertDialog in this way:
builder = new AlertDialog.Builder(getApplicationContext());
But the context was not correct and caused the error. I've changed it using the application context in this way:
In declare section:
Context mContext;
in the onCreate method
mContext = this;
And finally in the code where I need the AlertDialog:
start_stop = (Button) findViewById(R.id.start_stop);
start_stop.setOnClickListener( new View.OnClickListener()
{
#Override
public void onClick(View v)
{
if (!is_running)
{
builder = new AlertDialog.Builder(mContext);
builder.setMessage("MYTEXT")
.setCancelable(false)
.setPositiveButton("SI", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Task_Started = false;
startTask();
}
})
.setNegativeButton("NO",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
}
This is the solution for me.
I was getting this same problem. Because i was creating custom navigation drawer. But i forget to mention theme in my manifest like this
android:theme="#style/Theme.AppCompat.NoActionBar"
As soon i added the above the theme to my manifest it resolved the problem.
I have faced same problem.
If you are providing context to any class or method then provide YourActivityName.this instead of getApplicationContext().
Do this
builder = new AlertDialog.Builder(YourActivity.this);
Instead of
builder = new AlertDialog.Builder(getApplicationContext());
Change your theme style parent to
parent="Theme.AppCompat"
This worked for me ...
This one worked for me:
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/Theme.AppCompat.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Your Activity is extending ActionBarActivity which requires the AppCompat.theme to be applied.
Change from ActionBarActivity to Activity or FragmentActivity, it will solve the problem.
If you use no Action bar then :
android:theme="#android:style/Theme.Light.NoTitleBar.Fullscreen"

Making YesActionBar and NoActionBar theme in Android and using them in application

I would like to create simple activities in my android application and divide them into no-action bar and action-bar activities. To achieve this goal, I created 2 styles in styles.xml file:
<style name="AppTheme" parent="Theme.AppCompat.Light">
</style>
<style name="NoActionBar" parent="Theme.AppCompat.NoActionBar">
</style>
Additionally, my android manifest contains following lines of code:
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
because I want to use AppTheme by default. How can I use NoActionBar style in my activities?
I have tried using it as: style="#style/NoActionBar" and android:theme="#style/NoActionBar" but without any success.
I would like to override default style by editing .xml files. Could you please help me with that?
My current API level is 11
You can remove the ActionBar in code, by setting it invisible (well, it's really gone) in the onCreate() event of your "no-action" Activity/es.
Or in the onCreateView() event of your "no-action" Fragment/s.
ActionBar actionBar = getActionBar();
actionBar.hide();
Source: http://developer.android.com/guide/topics/ui/actionbar.html#Removing
You can put the style in the application tag which will apply to ALL activities in the app. Then, to specify which activities use the specific themes, you can add that same line of code after each activity tag.
Or, in your class (code not xml), you can specify with:
#Override
protected void onCreate(Bundle savedInstanceState) {
//Default method calls
setTheme(R.style.AppTheme);
super.onCreate(savedInstanceState);
I would also suggest using this as the parent theme instead: #android:style/Theme.Holo.Light.NoActionBar

Categories