Encountered this error trying to style the ActionBar title, on API 10, using the latest v7 appcompat support lib (r21, Lollipop). Works fine on API 15+ though.
my same code used to work fine until this r21 came along, causing many problems.
stack trace:
11-05 02:08:49.628: E/AndroidRuntime(527): Caused by: java.lang.UnsupportedOperationException: Can't convert to color: type=0x2
11-05 02:08:49.628: E/AndroidRuntime(527): at android.content.res.TypedArray.getColor(TypedArray.java:326)
11-05 02:08:49.628: E/AndroidRuntime(527): at android.widget.TextView.setTextAppearance(TextView.java:1616)
11-05 02:08:49.628: E/AndroidRuntime(527): at android.support.v7.widget.Toolbar.setTitle(Toolbar.java:579)
11-05 02:08:49.628: E/AndroidRuntime(527): at android.support.v7.internal.widget.ToolbarWidgetWrapper.setTitleInt(ToolbarWidgetWrapper.java:285)
11-05 02:08:49.628: E/AndroidRuntime(527): at android.support.v7.internal.widget.ToolbarWidgetWrapper.setWindowTitle(ToolbarWidgetWrapper.java:267)
11-05 02:08:49.628: E/AndroidRuntime(527): at android.support.v7.internal.widget.ActionBarOverlayLayout.setWindowTitle(ActionBarOverlayLayout.java:679)
11-05 02:08:49.628: E/AndroidRuntime(527): at android.support.v7.app.ActionBarActivityDelegateBase.onTitleChanged(ActionBarActivityDelegateBase.java:461)
11-05 02:08:49.628: E/AndroidRuntime(527): at android.support.v7.app.ActionBarActivity.onTitleChanged(ActionBarActivity.java:176)
11-05 02:08:49.628: E/AndroidRuntime(527): at android.app.Activity.onPostCreate(Activity.java:916)
11-05 02:08:49.628: E/AndroidRuntime(527): at android.app.Instrumentation.callActivityOnPostCreate(Instrumentation.java:1108)
My app's res/styles.xml:
<style name="AppBaseTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
<item name="actionBarStyle">#style/AppBaseTheme.ActionBar</item>
</style>
<style name="AppBaseTheme.ActionBar" parent="Widget.AppCompat.Light.ActionBar.Solid">
<item name="titleTextStyle">#style/TextAppearance.AppCompat.Widget.ActionBar.Title.Inverse</item>
</style>
v7 appcompat res/styles_base_text.xml:
53 <style name="Base.TextAppearance.AppCompat.Title">
54 <item name="android:textSize">#dimen/abc_text_size_title_material</item>
55 <item name="android:textColor">?android:textColorPrimary</item>
56 </style>
57
58 <style name="Base.TextAppearance.AppCompat.Title.Inverse">
59 <item name="android:textColor">?android:attr/textColorPrimaryInverse</item>
60 <item name="android:textColorHint">?android:attr/textColorHintInverse</item>
61 <item name="android:textColorHighlight">?android:attr/textColorHighlightInverse</item>
62 <item name="android:textColorLink">?android:attr/textColorLinkInverse</item>
63 </style>
However, once I removed the ".Inverse" in my styles.xml, it works fine.
It crashes at this line http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.3_r1/android/widget/TextView.java/#1616
while trying to get the colorHightlight.
Any idea how to fix?
It may be that in API 10 there is no Base.TextAppearance.AppCompat.Title.Inverse theme.
You should check if the theme is present in API 10.
A solution may be to define a common base theme that inherits from a different parent theme included in API 10.
res/values-v10/themes.xml:
<style name="Base.TextAppearance.AppCompat.Title">
<item name="android:textColor">?android:attr/textColorPrimaryInverse</item>
<item name="android:textColorHint">?android:attr/textColorHintInverse</item>
<item name="android:textColorHighlight">16842905</item>
<item name="android:textColorLink">?android:attr/textColorLinkInverse</item>
</style>
You can definitely see that textColorHighlightInverse is not present in API level 10.
Check this textColorHighlightInverse link and select API level 10 in left menu.
Also have a look at this https://code.google.com/p/android/issues/detail?id=18659
Related
I am having a problem implementing Material Button and I get the following error shown below. I have tried different solutions found on stackoverflow but can't seem to know why it keeps showing me this error. Any help would be much appreciated. Thanks in advance
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.app.lifeadvicequotes, PID: 8603
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.lifeadvicequotes/com.app.lifeadvicequotes.MainActivity}: android.view.InflateException: Binary XML file line #73 in com.app.lifeadvicequotes:layout/activity_main: Binary XML file line #73 in com.app.lifeadvicequotes:layout/activity_main: Error inflating class com.google.android.material.button.MaterialButton
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:4111)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4277)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2443)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:226)
at android.os.Looper.loop(Looper.java:313)
at android.app.ActivityThread.main(ActivityThread.java:8751)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1135)
Caused by: android.view.InflateException: Binary XML file line #73 in com.app.lifeadvicequotes:layout/activity_main: Binary XML file line #73 in com.app.lifeadvicequotes:layout/activity_main: Error inflating class com.google.android.material.button.MaterialButton
Caused by: android.view.InflateException: Binary XML file line #73 in com.app.lifeadvicequotes:layout/activity_main: Error inflating class com.google.android.material.button.MaterialButton
This is my style xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:textColorPrimary">#color/text_color_primary</item>
</style>
<!-- Base application theme. -->
<style name="AppTheme1" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="AppTheme.Button" parent="android:Widget.Material.Button">
<item name="colorButtonNormal">#FFFFFF</item>
<item name="android:textColor">#000000</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="NavigationDrawerStyle">
<item name="android:textSize">16sp</item>
</style>
</resources>
The issue might be from parent="android:Widget.Material.Button" try changing it to parent="Widget.MaterialComponents.Button or parent="Base.Widget.AppCompat.Button" if you haven't imported Material Components library
I looked at other SO posts and configured accordingly. But I still see this issue on some Android devices (4.3.1). It works 75% of time.
You need to use a Theme.AppCompat theme (or descendant) with this activity.
What is wrong with code below?
BaseActivity extends android.support.v7.appAppCompatActivity
<application
android:allowBackup="true"
android:theme="#style/AppTheme" >
All activities (crashed acitivity as well) use AppTheme from application tag. One activity uses:
<activity
android:name=".Activities.EntryActivity"
android:noHistory="true"
android:theme="#style/SplashTheme" >
I don't have any other style.xml.
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
</style>
<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowBackground">#drawable/background_splash</item>
</style>
From Android:
<style name="Theme.AppCompat.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
java.lang.RuntimeException: Unable to start activity
ComponentInfo{Activities.MyAcitivity}:
java.lang.IllegalStateException: You need to use a Theme.AppCompat
theme (or descendant) with this activity. 1 at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
2 at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
3 at android.app.ActivityThread.access$600(ActivityThread.java:141)
4 at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
5 at android.os.Handler.dispatchMessage(Handler.java:99) 6 at
android.os.Looper.loop(Looper.java:137) 7 at
android.app.ActivityThread.main(ActivityThread.java:5103) 8 at
java.lang.reflect.Method.invokeNative(Native Method) 9 at
java.lang.reflect.Method.invoke(Method.java:525) 10 at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12 at com.android.internal.os.ZygoteInit.main(Native Method) 13 at
dalvik.system.NativeStart.main(Native Method) 14 Caused by:
java.lang.IllegalStateException: You need to use a Theme.AppCompat
theme (or descendant) with this activity. 15 at
android.support.v7.app.AppCompatDelegateImplV7.i(SourceFile:340) 16 at
android.support.v7.app.AppCompatDelegateImplV7.h(SourceFile:309) 17 at
android.support.v7.app.AppCompatDelegateImplV7.setContentView(SourceFile:273)
18 at
android.support.v7.app.AppCompatActivity.setContentView(SourceFile:136)
19 at .Commons.BaseActivity.onCreate(SourceFile:236) 20 at
.MyActivity.onCreate(SourceFile:24) 21 at
android.app.Activity.performCreate(Activity.java:5133) 22 at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
23 at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
24 ... 12 more 25 java.lang.IllegalStateException: You need to use a
Theme.AppCompat theme (or descendant) with this activity. 26 at
android.support.v7.app.AppCompatDelegateImplV7.i(SourceFile:340) 27 at
android.support.v7.app.AppCompatDelegateImplV7.h(SourceFile:309) 28 at
android.support.v7.app.AppCompatDelegateImplV7.setContentView(SourceFile:273)
29 at
android.support.v7.app.AppCompatActivity.setContentView(SourceFile:136)
30 at Activities.Commons.BaseActivity.onCreate(SourceFile:236) 31 at
Activities.Accounts.AppLaunchActivity.onCreate(SourceFile:24) 32 at
android.app.Activity.performCreate(Activity.java:5133) 33 at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
34 at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
35 at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
36 at android.app.ActivityThread.access$600(ActivityThread.java:141)
37 at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
38 at android.os.Handler.dispatchMessage(Handler.java:99) 39 at
android.os.Looper.loop(Looper.java:137) 40 at
android.app.ActivityThread.main(ActivityThread.java:5103) 41 at
java.lang.reflect.Method.invokeNative(Native Method) 42 at
java.lang.reflect.Method.invoke(Method.java:525) 43 at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
44 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
45 at com.android.internal.os.ZygoteInit.main(Native Method) 46 at
dalvik.system.NativeStart.main(Native Method)
try this:
in res/values/styles.xml:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
Now in your manifest:
<application
android:theme="#style/AppTheme"> //base Application theme
<activity
android:name=".Activities.EntryActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar"> //Activity Theme
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
I use Google's support libraries of version 23.1.0.
compile 'com.android.support:appcompat-v7:23.1.0'
compile 'com.android.support:recyclerview-v7:23.1.0'
compile 'com.android.support:cardview-v7:23.1.0'
compile 'com.android.support:preference-v7:23.1.0'
compile 'com.android.support:design:23.1.0'
I am reusing a theme file that worked with versions of the support libraries prior to the introduction of material design.
<style name="AppTheme.StatusBarOverlay.Main" parent="#style/AppTheme.Main">
<item name="android:windowContentOverlay">#null</item>
</style>
<style name="AppTheme.Main" parent="#style/AppBaseTheme.Main">
<item name="windowActionBar">false</item>
</style>
<style name="AppBaseTheme.Main" parent="#style/Theme.AppCompat.Main">
<item name="colorPrimary">#color/main_color</item>
<item name="colorPrimaryDark">#color/main_color_dk</item>
</style>
<style name="Theme.AppCompat.Main" parent="#style/Base.Theme.AppCompat.Main" />
<style name="Base.Theme.AppCompat.Main" parent="#style/Base.V7.Theme.AppCompat" />
<style name="Theme.CustomDialog" parent="#android:style/Theme.Dialog">
<item name="android:windowBackground">#drawable/trans_draw</item>
<item name="android:windowNoTitle">true</item>
</style>
I made my MainActivity use the style by setting the theme in the manifest
<activity
android:name=".activities.MainActivity"
android:label="#string/title_activity_main"
android:theme="#style/AppTheme.StatusBarOverlay.Main" >
</activity>
When the action bar was retrieved
ActionBar actionBar = getSupportActionBar();
the following exception was raised.
Caused by: java.lang.IllegalArgumentException: AppCompat does not support the current theme features: { windowActionBar: false, windowActionBarOverlay: false, android:windowIsFloating: false, windowActionModeOverlay: false, windowNoTitle: false }
at android.support.v7.app.AppCompatDelegateImplV7.createSubDecor(AppCompatDelegateImplV7.java:423)
at android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:280)
at android.support.v7.app.AppCompatDelegateImplV7.initWindowDecorActionBar(AppCompatDelegateImplV7.java:173)
at android.support.v7.app.AppCompatDelegateImplBase.getSupportActionBar(AppCompatDelegateImplBase.java:89)
at android.support.v7.app.AppCompatActivity.getSupportActionBar(AppCompatActivity.java:79)
at com.dynamicwebapac.commerce.mobile.custom.CustomActivity.setupActionBar(CustomActivity.java:39)
at com.dynamicwebapac.commerce.mobile.custom.CustomActivity.onCreate(CustomActivity.java:25)
at com.dynamicwebapac.commerce.mobile.activities.MainActivity.onCreate(MainActivity.java:181)
at android.app.Activity.performCreate(Activity.java:5933)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
What happened to the Android support library after material design was introduced? How can I solve my current problem?
Using the new material support library, we set up themes like this styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
Also in the manifest
android:theme="#style/AppTheme"
And then we use the Toolbar as the action bar .Do this in your parent layout
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary" />
And we set it up in our Java source using
Toolbar toolbar=(Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Don't worry it happens with most of us. I'd like to start with reason of this problem.
The Material Design is in the favour of using Toolbar which is more flexible,...etc hence the themes or styles provided by AppCompat library are without an ActionBar or you can say a support ActionBar is provided and hence e need to disable the system default ActionBar.
Now follow any of the 2 steps below as suggested by Colns Abt:
Add any style which has NoActionBar at the ending like Theme.AppCompat.NoActionBar
OR
Add the following lines in your custom style
<item name="windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
Further follow steps given by Colns Abt.
From one android.support.v7.app.ActionBarActivity I'm starting another android.support.v7.app.ActionBarActivity activity, but getting this Exception:
FATAL EXCEPTION: main
Process: com.playgong.stg.dbg, PID: 32299
java.lang.ClassCastException: android.support.v7.internal.widget.ActionBarOverlayLayout$LayoutParams cannot be cast to com.android.internal.widget.ActionBarOverlayLayout$LayoutParams
at com.android.internal.widget.ActionBarOverlayLayout.applyInsets(ActionBarOverlayLayout.java:172)
at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:317)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
at android.view.View.measure(View.java:16497)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1912)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1109)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1291)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:996)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5600)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
at android.view.Choreographer.doCallbacks(Choreographer.java:574)
at android.view.Choreographer.doFrame(Choreographer.java:544)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
First activity has style
<style name="PS" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowContentTransitions">true</item>
</style>
Second (that which is crashing):
<style name="PS.Second" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:actionBarStyle">#style/PS.ActionBar</item>
<item name="android:windowNoTitle">false</item>
<item name="android:windowActionBar">true</item>
</style>
Any ideas how to avoid ClassCastException?
I am not sure what is PS.ActionBar style. So I commented that out. And tried the same thing on my Device Moto G API 21. I don't see the crash.
That being said, this crash appears to be an internal one. Besides ActionBarActivity has been deprecated, instead use AppCompatActivity.
PS: I am still waiting for your answer, so I could edit my answer with more findings.
I got this error because I was not using a activity theme which has no action bar. Using the following theme set in the manifest for the activity works:
<style name="mytheme.NoActionBar" parent="AppTheme">
<item name="android:windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
</style>
the other pecular thing is that it only occured on kitkat.
I applied the below style for timepicker:
<style name="MyHoloTimePicker" parent="#android:style/Widget.DeviceDefault.DatePicker">
<item name="android:textColor">#2eb135</item>
<item name="android:textSize">16sp</item>
<item name="android:textStyle">normal</item>
</style>
I have set the above style to time picker in xml as follows:
<TimePicker
android:id="#+id/pickertime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/MyHoloTimePicker"
/>
I got the below exception:
12-03 01:12:26.742: E/AndroidRuntime(9279): Caused by: java.lang.NullPointerException <br>
12-03 01:12:26.742: E/AndroidRuntime(9279): Caused by: java.lang.reflect.InvocationTargetException
How can I sort out this issue?
There is no Widget.DeviceDefault.DatePicker. Try setting the parent to:
parent="#android:style/Widget.DatePicker"
EDIT:
I found that the DeviceDefault themes are in a different file and can by used by API 14+.
themes_device_defaults.xml