I'm trying to implement the Toolbar using AppCompat v21 following this guide, http://antonioleiva.com/material-design-everywhere/ however I am unable to launch on KitKat or Lollipop and get the same error message (below). I fully believe I have the theme set up correctly but I am stuck at this point and have been for over a day. I've searched unsuccessfully for the solution so now I'm asking for some help. Thanks!
> 11-29 06:08:46.892 29771-29771/com.chaoticearth.chaoticearth E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.ce.ce, PID: 29771
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ce.ce/com.ce.ce.DrawerActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2212)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2271)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5146)
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:796)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:151)
at android.support.v7.app.ActionBarActivityDelegateBase.onCreate(ActionBarActivityDelegateBase.java:138)
at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:123)
at com.ce.ce.BaseActivity.onCreate(BaseActivity.java:16)
at com.ce.ce.DrawerActivity.onCreate(DrawerActivity.java:92)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2271)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5146)
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:796)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
at dalvik.system.NativeStart.main(Native Method)
Here is my /res/values/themes.xml
<resources>
<style name="CETheme" parent="AppTheme" ></style>
<style name="AppTheme" parent="Theme.AppCompat">
<item name="android:colorPrimaryDark">#color/MidnightBlue</item>
<item name="android:colorAccent">#color/LightYellow</item>
<item name="android:windowNoTitle">true</item>
<item name="windowActionBar">false</item>
</style>
Here is my /res/values-v21/themes.xml
<?xml version="1.0" encoding="utf-8"?>
<item name="android:windowContentTransitions">true</item>
<item name="android:windowAllowEnterTransitionOverlap">true</item>
<item name="android:windowAllowReturnTransitionOverlap">true</item>
<item name="android:windowSharedElementEnterTransition">#android:transition/move</item>
<item name="android:windowSharedElementExitTransition">#android:transition/move</item>
<item name="android:colorPrimary">#color/ChaosBlue</item>
<item name="android:colorPrimaryDark">#color/MidnightBlue</item>
<item name="android:colorAccent">#color/LightYellow</item>
<item name="android:statusBarColor">#color/ChaosBlue</item>
</style>
Here is a snippet from my DrawerActivity.java where the error is being thrown:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setActionBarIcon(R.drawable.ic_drawer);
And a snippet from the BaseActivity.java that is being called out:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayoutResource());
toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
}
And finally here is a snippet from my AndroidManifest.xml
<application
android:allowBackup="true"
android:icon="#drawable/logo"
android:label="#string/app_name"
android:theme="#style/CETheme"
android:name="android.support.multidex.MultiDexApplication">
I finally stumbled onto the reason, in my AndroidManifest I was setting a theme for my DrawerActivity, this was causing the error.
Try replacing android:colorPrimaryDark with colorPrimaryDark and android:colorAccent with colorAccent in your res/values/themes.xml.
Related
Whenever I show a Toast, the app crashes.
The app works fine if I use older version of AppCompat library or remove fontFamily from the style.
onCreate:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toast.makeText(this, "Test", Toast.LENGTH_SHORT).show(); //line 13
}
Dependency:
compile 'com.android.support:appcompat-v7:26.1.0'
AppTheme:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:fontFamily">sans-serif-light</item>
</style>
Stack Trace:
Caused by: java.lang.ArrayIndexOutOfBoundsException: length=16; index=233
at android.content.res.StringBlock.get(StringBlock.java:65)
at android.content.res.XmlBlock$Parser.getPooledString(XmlBlock.java:458)
at android.content.res.TypedArray.loadStringValueAt(TypedArray.java:1212)
at android.content.res.TypedArray.getString(TypedArray.java:202)
at android.support.v7.widget.TintTypedArray.getString(TintTypedArray.java:143)
at android.support.v7.widget.AppCompatTextHelper.updateTypefaceAndStyle(AppCompatTextHelper.java:215)
at android.support.v7.widget.AppCompatTextHelper.loadFromAttributes(AppCompatTextHelper.java:152)
at android.support.v7.widget.AppCompatTextHelperV17.loadFromAttributes(AppCompatTextHelperV17.java:38)
at android.support.v7.widget.AppCompatTextView.(AppCompatTextView.java:81)
at android.support.v7.widget.AppCompatTextView.(AppCompatTextView.java:71)
at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:103)
at android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1024)
at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1081)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:769)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
at android.widget.Toast.makeText(Toast.java:266)
at io.yarsa.blankapp.MainActivity.onCreate(MainActivity.java:13)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6126)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Is there any alternative so that I can use the fontFamily attribute in theme using the latest version of AppCompat library?
Add font in Theme like below-
<style name="AppTheme" parent="Base.Theme.AppCompat.Light.DarkActionBar">
<item name="android:textViewStyle">#style/TextViewStyle</item>
<item name="android:buttonStyle">#style/ButtonStyle</item>
</style>
<style name="TextViewStyle" parent="android:Widget.TextView">
<item name="android:fontFamily">sans-serif-light</item>
</style>
<style name="ButtonStyle" parent="Widget.AppCompat.Button">
<item name="android:fontFamily">sans-serif-light</item>
</style>
According to the Android Developer Guide for fonts in xml
Adding fonts to style
Open the styles.xml, and set the fontFamily attribute to the font file
you want to access.
<style name="customfontstyle" parent="#android:style/TextAppearance.Small">
<item name="android:fontFamily">#font/lobster</item>
</style>
In your case you should put the #font/ prefix
<item name="android:fontFamily">#font/sans-serif-light</item>
I tried change a color by use a Material Design method.
But after this, when I want to run my application it's stopped work.
When I click app icon to run this one, I see this app for circa 1 second only.
I see that colors were changed but this application stops work properly.
I don't know where is a problem because I use instructions on official Android's website.
Could someone help me to resolve this problem?
Below is my code:
Styles.xml:
<!-- Base application theme. -->
<style name="AppTheme" parent="android:Theme.Material">
<!-- Customize your theme here. -->
<item name="android:colorPrimary">#color/primaryColor</item>
<item name="android:colorPrimaryDark">#color/colorprimarydark</item>
<item name="android:windowBackground">#color/windowbackground</item>
<item name="android:textColorPrimary">#color/textcolorprimary</item>
</style>
Colors.xml:
<color name="primaryColor">#FF9800</color>
<color name="colorprimarydark">#FF3F00</color>
<color name="windowbackground">#BDBDBD</color>
<color name="textcolorprimary">#039BE5</color>
Activity.xml:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.example.karol.material_test.MainActivity">
<TextView
android:text="Hello World!"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
Logcat:
01-20 21:27:12.120 28957-28957/com.example.karol.material_test E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.karol.material_test, PID: 28957
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.karol.material_test/com.example.karol.material_test.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2406)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2466)
at android.app.ActivityThread.access$1200(ActivityThread.java:152)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1341)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5538)
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:960)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:151)
at android.support.v7.app.ActionBarActivityDelegateBase.onCreate(ActionBarActivityDelegateBase.java:138)
at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:123)
at com.example.karol.material_test.MainActivity.onCreate(MainActivity.java:12)
at android.app.Activity.performCreate(Activity.java:6013)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2359)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2466)
at android.app.ActivityThread.access$1200(ActivityThread.java:152)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1341)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5538)
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:960)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
01-20 21:27:12.510 28957-28957/com.example.karol.material_test I/Process: Sending signal. PID: 28957 SIG: 9
You need to use a Theme.AppCompat theme (or descendant) with this
activity.
Instead of: android:Theme.Material
Check this:
https://stackoverflow.com/a/21815015/4409113
And just use AppCompat in your styles or AppCompatActivity in the Java codes.
I use the android.support library for my project, I have developed my app using android 4.4 and everything works good but my problem comes when I try to run the application in a device with Android Lollipop.
When it loads the Activity with the Toolbar, the application stops and returns this error.
Error inflating class android.support.v7.widget.Toolbar
I have found the same problem on the internet but I haven't found the solution in the answers.
This is my styles.xml
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<!-- Base theme applied no matter what API -->
<item name="android:windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="colorPrimary">#color/primary</item>
<item name="colorPrimaryDark">#color/dark_primary</item>
<item name="colorAccent">#color/accent_color</item>
</style>
Here is my styles.xml (v22)
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="android:Theme.Material.Light.NoActionBar">
<item name="colorPrimary">#color/primary</item>
<item name="colorPrimaryDark">#color/dark_primary</item>
<item name="colorAccent">#color/accent_color</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
</resources>
To define the toolbar in .xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/mitoolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
local:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
local:popupTheme="#style/ThemeOverlay.AppCompat.Light"
tools:showIn="#layout/activity_main" />
And in my MainActivty I just include that toolbar:
<include layout="#layout/toolbar" />
And finally the header of my MainActivity.java
public class MainActivity extends android.support.v7.app.ActionBarActivity
implements NavigationDrawerFragment.NavigationDrawerCallbacks {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //Here stops my application
}
Heres the logcat
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fitness.dullmonkey.keepingfit/com.fitness.dullmonkey.keepingfit.MainActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class android.support.v7.widget.Toolbar
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2581)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2656)
at android.app.ActivityThread.access$800(ActivityThread.java:178)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1512)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5691)
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:959)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class android.support.v7.widget.Toolbar
at android.view.LayoutInflater.createView(LayoutInflater.java:633)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
at android.view.LayoutInflater.parseInclude(LayoutInflater.java:892)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:802)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:249)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106)
at com.fitness.dullmonkey.keepingfit.MainActivity.onCreate(MainActivity.java:40)
at android.app.Activity.performCreate(Activity.java:6142)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1115)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2528)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2656)
at android.app.ActivityThread.access$800(ActivityThread.java:178)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1512)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5691)
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:959)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
at android.view.LayoutInflater.createView(LayoutInflater.java:607)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
at android.view.LayoutInflater.parseInclude(LayoutInflater.java:892)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:802)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:249)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106)
at com.fitness.dullmonkey.keepingfit.MainActivity.onCreate(MainActivity.java:40)
at android.app.Activity.performCreate(Activity.java:6142)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1115)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2528)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2656)
at android.app.ActivityThread.access$800(ActivityThread.java:178)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1512)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5691)
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:959)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
Caused by: java.lang.RuntimeException: Failed to resolve attribute at index 216
at android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:569)
at android.view.View.<init>(View.java:4084)
at android.view.ViewGroup.<init>(ViewGroup.java:524)
at android.view.ViewGroup.<init>(ViewGroup.java:520)
at android.support.v7.widget.Toolbar.<init>(Toolbar.java:195)
at android.support.v7.widget.Toolbar.<init>(Toolbar.java:191)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
at android.view.LayoutInflater.createView(LayoutInflater.java:607)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
at android.view.LayoutInflater.parseInclude(LayoutInflater.java:892)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:802)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:249)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106)
at com.fitness.dullmonkey.keepingfit.MainActivity.onCreate(MainActivity.java:40)
at android.app.Activity.performCreate(Activity.java:6142)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1115)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2528)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2656)
at android.app.ActivityThread.access$800(ActivityThread.java:178)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1512)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5691)
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:959)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
Thank you in advance for your help.
The cause of the error is that, in the toolbar xml, the ?attr prefix refers to attributes in support library themes. On the other hand, under v22 of style.xml above, the system theme android:Theme.Material.Light.NoActionBar instead of a support library theme is used. The inflater therefore cannot resolve attributes of the support library such as ?attr/actionBarSize.
Using the support library theme Theme.AppCompat.Light.NoActionBar in style.xml (regardless of version) addresses the issue.
in my case the same error but no symptoms of ?attr in android axml file
it looks like a compatibility issue of Xamarin.Appcombat (I found somewhere in web)
try to deploy the same project into lower Android API
in my case Android simulator API 21 had the error above , but another simulator with Android API 18 had no problem at all
I have developed an app that uses YouTube android API to play a certain video inside the app. At that time I have used as target API 19, and also target API 19. Now I want do change the design of the app switching to Material Design using target API 22 and also using AppCompat-v7 :22+ library. But the YouTubeBaseActivity cant be opened and the application crashes. Below I will post what I have done so far.
EventDetail.java
public class EventDetail extends YouTubeBaseActivity implements
YouTubePlayer.OnInitializedListener { .
.
.
protected void onCreate(Bundle savedInstanceState) {
// remove the shadow from action bar
setTheme(R.style.Theme_TranslucentActionBar_ActionBar_NoOverlay);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_event_detail);
// enable back button
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// setting the action bar initially transparent
mActionBarBackgroundDrawable = new ColorDrawable(
Color.parseColor("#ec5c59"));
mActionBarBackgroundDrawable.setAlpha(0);
getSupportActionBar().setBackgroundDrawable(mActionBarBackgroundDrawable);
// binding the scrollview
...
Manifest File
<activity
android:name="com.dev.apk.evente.al.EventDetail"
android:icon="#drawable/ikona_projekte"
android:label="#string/title_activity_event_detail"
android:screenOrientation="portrait"
android:theme="#style/Theme.TranslucentActionBar.ActionBar.Overlay" />
Layut File:
<RelativeLayout
android:id="#+id/video_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:paddingLeft="23dp"
android:paddingRight="23dp"
android:paddingBottom="15dp"
android:background="#drawable/event_background_block" >
<TextView
android:id="#+id/video_label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="#color/white"
android:gravity="left"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:text="Video"
android:textColor="#color/VeryDarkGray"
android:textSize="22sp" >
</TextView>
<com.google.android.youtube.player.YouTubePlayerView
android:id="#+id/youtube_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/video_label"
android:background="#color/white" />
</RelativeLayout>
Theme.xml File
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.TranslucentActionBar" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:actionBarStyle">#style/Widget.ActionBar</item>
</style>
<style name="Theme.TranslucentActionBar.ActionBar" />
<style name="Theme.TranslucentActionBar.ActionBar.Overlay">
<item name="android:actionBarStyle">#style/Widget.ActionBar.Transparent</item>
<item name="android:windowActionBarOverlay">true</item>
<item name="colorPrimary">#color/evente_date_color</item>
<item name="colorPrimaryDark">#color/evente_darken_color</item>
</style>
<!-- Theme without ActionBar shadow (inherits main theme) -->
<style name="Theme.TranslucentActionBar.ActionBar.NoOverlay" parent="Theme.TranslucentActionBar.ActionBar.Overlay">
<item name="android:windowContentOverlay">#null</item>
</style>
</resources>
So at the beginning it doesn't open because the method getActionBar() return Null, then i changed it to getSupportActionBar() but i doesn't recognized it. Then at the end I changed this line in the following:
public class EventDetail extends AppCompatActivity implements
YouTubePlayer.OnInitializedListener {
and it gives me the following error:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myikub.al/com.dev.apk.evente.al.EventDetail}: android.view.InflateException: Binary XML file line #250: Error inflating class com.google.android.youtube.player.YouTubePlayerView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2342)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
at android.app.ActivityThread.access$600(ActivityThread.java:148)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:5406)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #250: Error inflating class com.google.android.youtube.player.YouTubePlayerView
at android.view.LayoutInflater.createView(LayoutInflater.java:620)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:249)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106)
at com.dev.apk.evente.al.EventDetail.onCreate(EventDetail.java:112)
at android.app.Activity.performCreate(Activity.java:5303)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2296)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
at android.app.ActivityThread.access$600(ActivityThread.java:148)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:5406)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
at android.view.LayoutInflater.createView(LayoutInflater.java:594)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:249)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106)
at com.dev.apk.evente.al.EventDetail.onCreate(EventDetail.java:112)
at android.app.Activity.performCreate(Activity.java:5303)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2296)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
at android.app.ActivityThread.access$600(ActivityThread.java:148)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:5406)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: A YouTubePlayerView can only be created with an Activity which extends YouTubeBaseActivity as its context.
at com.google.android.youtube.player.YouTubePlayerView.<init>(Unknown Source)
....
I am using Android Studio. Any idea will be appreciated.
The problem is that you're using the YouTubePlayerView, which requires your Activity to extend YouTubeBaseActivity. Then you changed your activity, so that it now extends AppCompatActivity. This breaks YouTubePlayerView.
The solution is pretty simple: use the YouTubePlayerFragment class. This does not pose any requirement on the Activity, leaving you with plenty of options for your theming.
According to the stack trace the activity has to extend YouTubeBaseActivity when using the YouTube API
If you need to have an actionbar then I think the best route is to change the style to have no actionbar and just use a toolbar in your view.
paste this code inside gradle app
compile 'com.google.android.gms:play-services:8.4.0'
compile files('libs/YouTubeAndroidPlayerApi.jar')
I am running my application in nexus 7 with api 21.When app opens from background
it force close with following exception
java.lang.UnsupportedOperationException: Can't convert to color: type=0x1
at android.content.res.TypedArray.getColor(TypedArray.java:404)
Log cat
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.online.Androidv2/com.online.Androidv2.PagerActivity}: java.lang.UnsupportedOperationException: Can't convert to color: type=0x1
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
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)
Caused by: java.lang.UnsupportedOperationException: Can't convert to color: type=0x1
at android.content.res.TypedArray.getColor(TypedArray.java:404)
at android.app.Activity.onApplyThemeResource(Activity.java:3674)
at android.view.ContextThemeWrapper.initializeTheme(ContextThemeWrapper.java:140)
at android.view.ContextThemeWrapper.setTheme(ContextThemeWrapper.java:85)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2244)
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)
I am using the theme AppTheme for the application.
<style name="AppTheme" parent="AppTheme.Base" />
<style name="AppTheme.Base" parent="Theme.AppCompat">
<item name="colorPrimary">#color/theme_color</item>
<item name="colorPrimaryDark">#color/theme_color_dark</item>
<item name="android:windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="drawerArrowStyle">#style/DrawerArrowStyle</item>
</style>
<style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle">
<item name="spinBars">true</item>
<item name="color">#android:color/white</item>
</style>
Color values is
<color name="theme_color">#b61702</color>
<color name="theme_color_dark">#a70001</color>
I can't find the reason for this.Any help?
I encountered same problem, After I check my code, I found I used #style/custom to set android:textColor in my style xml file.
Then I just refer #color/custom to android:textColor, error disappeared.
And error happened in Android API 18, API 23 works well.
useful link: java.lang.UnsupportedOperationException: Can't convert to color: type=0x1