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 have been trying to add google-cast to my Android app that plays videos using Exoplayer. The app has one single activity withTheme.Leanback theme and the same UI is used for both Phones and Android TV.
To add casting support on phones, I have modified in the player view and I added the cast button as below:
<androidx.mediarouter.app.MediaRouteButton
android:id="#+id/media_route_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:scaleY="0.8"
android:scaleX="0.8"
android:background="#android:color/transparent"
android:mediaRouteTypes="user"
android:visibility="visible" />
When I run the application I get the following error:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.EXAMPLE.PACKAGE, PID: 10661
android.view.InflateException: Binary XML file line #76 in com.EXAMPLE.PACKAGE:layout/activity_player: Binary XML file line #76 in com.EXAMPLE.PACKAGE:layout/activity_player: Error inflating class androidx.mediarouter.app.MediaRouteButton
Caused by: android.view.InflateException: Binary XML file line #76 in com.EXAMPLE.PACKAGE:layout/activity_player: Error inflating class androidx.mediarouter.app.MediaRouteButton
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
at android.view.LayoutInflater.createView(LayoutInflater.java:852)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:1004)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:959)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1121)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1082)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1124)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1082)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1124)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1082)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1124)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1082)
at android.view.LayoutInflater.inflate(LayoutInflater.java:680)
at android.view.LayoutInflater.inflate(LayoutInflater.java:532)
at com.EXAMPLE.PACKAGE.fragment.PlayerFragment.onCreateView(PlayerFragment.java:162)
at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2963)
at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:518)
at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:282)
at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2189)
at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2100)
at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:2002)
at androidx.fragment.app.FragmentManager$5.run(FragmentManager.java:524)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: java.lang.IllegalArgumentException: background can not be translucent: #0
at androidx.core.graphics.ColorUtils.calculateContrast(ColorUtils.java:161)
at androidx.mediarouter.app.MediaRouterThemeHelper.getControllerColor(MediaRouterThemeHelper.java:177)
at androidx.mediarouter.app.MediaRouterThemeHelper.getRouterThemeId(MediaRouterThemeHelper.java:313)
at androidx.mediarouter.app.MediaRouterThemeHelper.createThemedButtonContext(MediaRouterThemeHelper.java:107)
at androidx.mediarouter.app.MediaRouteButton.<init>(MediaRouteButton.java:153)
at androidx.mediarouter.app.MediaRouteButton.<init>(MediaRouteButton.java:149)
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
at android.view.LayoutInflater.createView(LayoutInflater.java:852)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:1004)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:959)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1121)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1082)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1124)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1082)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1124)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1082)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1124)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1082)
at android.view.LayoutInflater.inflate(LayoutInflater.java:680)
at android.view.LayoutInflater.inflate(LayoutInflater.java:532)
at com.EXAMPLE.PACKAGE.fragment.PlayerFragment.onCreateView(PlayerFragment.java:162)
at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2963)
at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:518)
at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:282)
at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2189)
at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2100)
at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:2002)
at androidx.fragment.app.FragmentManager$5.run(FragmentManager.java:524)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
I have tried setting the background of the castbusston transparent as was suggested in few places online but that did not help.
I can have several other Image Button and Text Buttons that are working fine.
I tried using <MediaRouteButton instead of <androidx.mediarouter.app.MediaRouteButton. This way the button will be showed and greyed out in the player layout however, when I try to intitize the cast context as below:
mMediaRouteButton = (MediaRouteButton) mView.findViewById(R.id.media_route_button);
CastButtonFactory.setUpMediaRouteButton(mContext, mMediaRouteButton);
mCastContext = CastContext.getSharedInstance(mContext);
I get the following error:
java.lang.ClassCastException: android.app.MediaRouteButton cannot be cast to androidx.mediarouter.app.MediaRouteButton
I have been puzzled by this issue for couple of days. Thank you very much in advance for your kind comments and suggestion.
Make sure that you have included the dependency that contains that class in your module's list of dependencies, such as:
implementation "androidx.mediarouter:mediarouter:1.2.6"
(or another version as appropriate)
The issue was that the primaryColor of the app theme that I was using had transparency channel, ie, #3d000000. However MediaRouteButton that is taking the primaryColor of theme needs a color without transparency channel such as #000000. So I resolved the issue as follow:
<style name="AppTheme" parent="#style/Theme.Leanback">
<!-- Primary Color -->
<item name="colorPrimary">#color/defaultColor</item>
</style>
where I defined <color name="defaultColor">#000000</color>
Later on I encountered another issue that MediaCastButton can only be used with AppCompat decedent theme. The solution for me was to change the app theme to an Theme.AppCompat.Light.NoActionBar theme and modify it to look like Theme.Leanback as below:
<style name="AppTheme1" parent="#style/Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/blackColor</item>
<item name="android:windowBackground">#color/blackColor</item>
<!-- <item name="mediaRouteButtonTint">#color/whiteColor</item>-->
<item name="baseCardViewStyle">#style/Widget.Leanback.BaseCardViewStyle</item>
<item name="imageCardViewStyle">#style/Widget.Leanback.ImageCardViewStyle</item>
<item name="imageCardViewImageStyle">#style/Widget.Leanback.ImageCardView.ImageStyle</item>
<item name="imageCardViewTitleStyle">#style/Widget.Leanback.ImageCardView.TitleStyle</item>
<item name="imageCardViewContentStyle">#style/Widget.Leanback.ImageCardView.ContentStyle</item>
<item name="imageCardViewBadgeStyle">#style/Widget.Leanback.ImageCardView.BadgeStyle</item>
<item name="imageCardViewInfoAreaStyle">#style/Widget.Leanback.ImageCardView.InfoAreaStyle</item>
<item name="browsePaddingStart">#dimen/lb_browse_padding_start</item>
<item name="browsePaddingEnd">#dimen/lb_browse_padding_end</item>
<item name="browsePaddingTop">#dimen/lb_browse_padding_top</item>
<item name="browsePaddingBottom">#dimen/lb_browse_padding_bottom</item>
<item name="browseRowsMarginStart">#dimen/lb_browse_rows_margin_start</item>
<item name="browseRowsMarginTop">#dimen/lb_browse_rows_margin_top</item>
<item name="browseRowsFadingEdgeLength">#dimen/lb_browse_rows_fading_edge</item>
<!-- <item name="headersVerticalGridStyle">#style/Widget.Leanback.Headers.VerticalGridView</item>-->
<!-- <item name="headerStyle">#style/Widget.Leanback.Header</item>-->
<!-- <item name="sectionHeaderStyle">#style/Widget.Leanback.Header.Section</item>-->
<item name="rowsVerticalGridStyle">#style/Widget.Leanback.Rows.VerticalGridView</item>
<item name="rowHorizontalGridStyle">#style/Widget.Leanback.Row.HorizontalGridView</item>
<item name="itemsVerticalGridStyle">#style/Widget.Leanback.GridItems.VerticalGridView</item>
<item name="browseTitleViewLayout">#layout/lb_browse_title</item>
<item name="browseTitleTextStyle">#style/Widget.Leanback.Title.Text</item>
<item name="browseTitleIconStyle">#style/Widget.Leanback.Title.Icon</item>
<item name="browseTitleViewStyle">#style/Widget.Leanback.TitleView</item>
<item name="rowHeaderStyle">#style/Widget.Leanback.Row.Header</item>
<item name="rowHeaderDescriptionStyle">#style/Widget.Leanback.Row.Header.Description</item>
<item name="rowHoverCardTitleStyle">#style/Widget.Leanback.Row.HoverCardTitle</item>
<item name="rowHoverCardDescriptionStyle">#style/Widget.Leanback.Row.HoverCardDescription</item>
<item name="rowHeaderDockStyle">#style/Widget.Leanback.Row.HeaderDock</item>
<!-- <item name="searchOrbViewStyle">#style/Widget.Leanback.SearchOrbViewStyle</item>-->
<item name="detailsDescriptionTitleStyle">#style/Widget.Leanback.DetailsDescriptionTitleStyle</item>
<item name="detailsDescriptionSubtitleStyle">#style/Widget.Leanback.DetailsDescriptionSubtitleStyle</item>
<item name="detailsDescriptionBodyStyle">#style/Widget.Leanback.DetailsDescriptionBodyStyle</item>
<item name="detailsActionButtonStyle">#style/Widget.Leanback.DetailsActionButtonStyle</item>
<!-- Attributes used for styling of a playback -->
<item name="playbackPaddingStart">#dimen/lb_playback_controls_margin_start</item>
<item name="playbackPaddingEnd">#dimen/lb_playback_controls_margin_end</item>
<item name="playbackMediaItemPaddingStart">#dimen/lb_playback_media_row_horizontal_padding</item>
<item name="playbackMediaListHeaderStyle">#style/Widget.Leanback.PlaybackMediaListHeaderStyle</item>
<item name="playbackMediaItemRowStyle">#style/Widget.Leanback.PlaybackMediaItemRowStyle</item>
<item name="playbackMediaItemSeparatorStyle">#style/Widget.Leanback.PlaybackMediaItemSeparatorStyle</item>
<item name="playbackMediaListHeaderTitleStyle">#style/Widget.Leanback.PlaybackMediaListHeaderTitleStyle</item>
<item name="playbackMediaItemDetailsStyle">#style/Widget.Leanback.PlaybackMediaItemDetailsStyle</item>
<item name="playbackMediaItemNumberViewFlipperStyle">#style/Widget.Leanback.PlaybackMediaItemNumberViewFlipperStyle</item>
<item name="playbackMediaItemNumberViewFlipperLayout">#layout/lb_media_item_number_view_flipper</item>
<item name="playbackMediaItemNumberStyle">#style/Widget.Leanback.PlaybackMediaItemNumberStyle</item>
<item name="playbackMediaItemNameStyle">#style/Widget.Leanback.PlaybackMediaItemNameStyle</item>
<item name="playbackMediaItemDurationStyle">#style/Widget.Leanback.PlaybackMediaItemDurationStyle</item>
<item name="playbackControlsButtonStyle">#style/Widget.Leanback.PlaybackControlsButtonStyle</item>
<item name="playbackControlButtonLabelStyle">#style/Widget.Leanback.PlaybackControlLabelStyle</item>
<item name="playbackControlsTimeStyle">#style/Widget.Leanback.PlaybackControlsTimeStyle</item>
<item name="playbackControlsActionIcons">#style/Widget.Leanback.PlaybackControlsActionIconsStyle</item>
<item name="playbackControlsAutoHideTimeout">#integer/lb_playback_controls_show_time_ms</item>
<item name="playbackControlsAutoHideTickleTimeout">#integer/lb_playback_controls_tickle_timeout_ms</item>
<item name="errorMessageStyle">#style/Widget.Leanback.ErrorMessageStyle</item>
<item name="defaultSearchColor">#color/lb_default_search_color</item>
<item name="defaultSearchIconColor">#color/lb_default_search_icon_color</item>
<item name="defaultSearchBrightColor">?attr/defaultSearchColor</item>
<item name="defaultSearchIcon">#drawable/lb_ic_in_app_search</item>
<item name="defaultSectionHeaderColor">?attr/defaultSearchColor</item>
<item name="overlayDimMaskColor">#color/lb_view_dim_mask_color</item>
<item name="overlayDimActiveLevel">#fraction/lb_view_active_level</item>
<item name="overlayDimDimmedLevel">#fraction/lb_view_dimmed_level</item>
</style>
I am having a crash with the following exception on Android device SM-J727U but not on other devices. I read through some stack overflow answers and it is effecting only some new android devices on Android 7.0.
Can any one suggest how to resolve it and what is causing it to happen.
Thanks
R
Here is the exception.
Terminating app due to an uncaught exception 'java.lang.UnsupportedOperationException', reason: 'Failed to resolve attribute at index 4: TypedValue{t=0x2/d=0x1010099 a=1}'
Last Exception Backtrace
0 android.view.InflateException: Binary XML file line #43: Binary XML file line #43: Error inflating class TextView
1 Caused by: android.view.InflateException: Binary XML file line #43: Error inflating class TextView
2 Caused by: java.lang.UnsupportedOperationException: Failed to resolve attribute at index 4: TypedValue{t=0x2/d=0x1010099 a=1}
3 at android.content.res.TypedArray.getColor(TypedArray.java:476)
4 at android.widget.TextView.<init>(TextView.java:1105)
5 at android.widget.TextView.<init>(TextView.java:1018)
6 at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:76)
7 at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:72)
8 at android.support.v7.app.AppCompatViewInflater.createTextView(AppCompatViewInflater.java:176)
9 at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:101)
10 at android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1035)
11 at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1092)
XML
<android.support.design.widget.TextInputLayout
android:id="#+id/til_user_full_names"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:theme="#style/EmployeeFieldLabel"
android:layout_below="#+id/tv_english_characters_only">
<EditText
android:id="#+id/user_full_names"
android:layout_width="match_parent"
style="#style/EmployeeFieldTextStyle"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="#string/names_as_it_appears_on_id"
android:inputType="textPersonName"/>
</android.support.design.widget.TextInputLayout>
Styles
<style name="EmployeeFieldLabel" parent="TextAppearance.AppCompat">
<!-- Hint color and label color in FALSE state -->
<item name="android:textColorHint">#9e9e9e</item>
<item name="android:textSize">#dimen/dp_text_size_medium</item>
<item name="colorControlActivated">#color/blueyPurple</item>
</style>
<style name="EmployeeFieldTextStyle">
<item name="android:textSize">#dimen/dp_text_size_medium</item>
<item name="android:textColor">#000000</item>
<item name="android:textCursorDrawable">#null</item>
<item name="fontPath">fonts/OpenSans-Regular.ttf</item>
<item name="android:background">#drawable/bg_field</item>
</style>
The cause for this is Google spell checker popup, enabled in some devices which causes red line under words in Edittext
Use this
In your Styles
<item name="colorControlHighlight"></item>
In Your xml
android:inputType="textNoSuggestions"
Instead of
android:inputType="textPersonName"
<item name="colorControlActivated">#color/blueyPurple</item>
I followed a tutorial of new component NavigationView in Support Design Library and can't get through this error message :
Error inflating class android.support.design.widget.NavigationView
I tried every workaround here
Error when using any Android Design Support Library Elements
but error message still exists.
xml
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<include layout="#layout/toolbar" android:id="#+id/mainToolBar" />
<fragment android:name="com.ais.cherry.fragment.LoginFragment"
android:id="#+id/loginFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/mainToolBar" />
<fragment android:name="com.ais.cherry.fragment.WaterFallFragment"
android:id="#+id/mainFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/mainToolBar" />
<fragment android:name="com.ais.cherry.fragment.SearchFragment"
android:id="#+id/searchFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/mainToolBar" />
<fragment android:name="com.ais.cherry.fragment.ChatMainFragment"
android:id="#+id/chatMainFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/mainToolBar" />
<fragment android:name="com.ais.cherry.fragment.ProfileFragment"
android:id="#+id/profileFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/mainToolBar" />
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#android:color/white"
app:itemTextColor="#212121"
app:headerLayout="#layout/drawer_header"
app:menu="#menu/drawer" />
</android.support.v4.widget.DrawerLayout>
build.gradle
buildscript {
repositories {
mavenCentral()
maven { url 'https://maven.fabric.io/public' }
}
dependencies {
classpath 'io.fabric.tools:gradle:1.+'
classpath 'com.android.tools.build:gradle:1.2.3'
}
}
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "com.ais.cherry"
minSdkVersion 16
targetSdkVersion 22
multiDexEnabled true
renderscriptTargetApi 22
renderscriptSupportModeEnabled true
}
dependencies {
compile 'com.android.support:appcompat-v7:22.2.0'
compile 'com.android.support:support-v4:22.2.0'
compile 'com.android.support:design:22.2.0'
compile 'com.android.support:recyclerview-v7:22.2.0'
}
Project build.gradle
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.2.3'
}
}
styles.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- Base application theme. -->
<style name="AppTheme" parent="AppTheme.Base">
</style>
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowActionBarOverlay">true</item>
<item name="android:windowActionModeOverlay">true</item>
<item name="android:actionModeStyle">#style/AppTheme.ActionModeStyle</item>
<item name="android:windowActionBar">false</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowNoTitle">true</item>
<!-- Support library compatibility -->
<item name="windowActionBarOverlay">true</item>
<!-- ActionBar color -->
<item name="colorPrimary">#E91E63</item>
<!-- Status bar color -->
<item name="colorPrimaryDark">#C2185B</item>
<!-- Window color -->
<item name="android:windowBackground">#color/white</item>
<!-- ActionBar title text -->
<item name="android:titleTextStyle">#style/AppTheme.MyActionBarTitleText</item>
<item name="colorAccent">#9E9E9E</item>
<item name="drawerArrowStyle">#style/AppTheme.MyDrawerArrowStyle</item>
<!-- color for actionMenu-->
<item name="actionMenuTextColor">#FFFFFF</item>
<item name="android:actionMenuTextColor">#FFFFFF</item>
<!-- android:textColorSecondary is the color of the menu
overflow icon (three vertical dots) -->
<item name="android:textColorPrimary">#212121</item>
<item name="android:textColorSecondary">#FFFFFF</item>
<!--Navigation bar color-->
<item name="android:navigationBarColor">#E91E63</item>
<!--Status bar color-->
<item name="android:statusBarColor">#C2185B</item>
</style>
Any help would be appreciated!
Edit -- add #menu/drawer & #menu/drawer_header & stacktrace
drawer.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_first_fragment"
android:icon="#drawable/home_view"
android:checked="true"
android:title="#string/main"/>
<item
android:id="#+id/nav_second_fragment"
android:icon="#drawable/comment_view"
android:title="#string/chat"/>
<item
android:id="#+id/nav_third_fragment"
android:icon="#drawable/user_view"
android:title="#string/profile"/>
<item android:title="#string/search">
<menu>
<item
android:title="#string/clothes"/>
<item
android:title="#string/pants"/>
</menu>
</item>
</group>
</menu>
drawer_header.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="192dp"
android:background="?attr/colorPrimaryDark"
android:padding="16dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark"
android:gravity="bottom">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/title"
android:textColor="#android:color/white"
android:textAppearance="#style/TextAppearance.AppCompat.Body1"/>
</LinearLayout>
StackTrace (only part of them)
It thrown out three exceptions as below : all points out that the problems is caused by the line of "setContentView(layout)" at in layout.xml.
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ais.cherry/com.ais.cherry.activity.FirstActivity}: android.view.InflateException: Binary XML file line #32: Error inflating class android.support.design.widget.NavigationView
Caused by: android.view.InflateException: Binary XML file line #32: Error inflating class android.support.design.widget.NavigationView
Caused by: java.lang.reflect.InvocationTargetException
Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x0
Actually it is not the matter of the primarycolortext, upgrading or downgrading the dependencies.This problem will likely occur when the version of your appcompat library and design support library doesn't match.
Example of matching condition
compile 'com.android.support:appcompat-v7:23.1.1' // appcompat library
compile 'com.android.support:design:23.1.1' //design support library
I had similar error.
When i use
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#673AB7</item>
<item name="colorPrimaryDark">#512DA8</item>
<item name="colorAccent">#00BCD4</item>
<item name="android:textColorPrimary">#212121</item>
<item name="android:textColorSecondary">#727272</item>
</style>
works for me when i remove the android:textColorPrimary and android:textColorSecondary theme items.
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#673AB7</item>
<item name="colorPrimaryDark">#512DA8</item>
<item name="colorAccent">#00BCD4</item>
</style>
Try working with a very simple App theme to start off with.
EDIT:
This tutorial will help.
My understanding is that using "android:textColorPrimary" requires minimum api level 21. Using the same tag without "android:" uses the design support library. Any support library widget will try to find the "textColorPrimary" item instead of "android:textColorPrimary" and if it fails to find the same it throws the above mentioned error.
I also had same error. In my case some of the resources were in drawable-v21 only. Copy those resources to drawable folder also. This solved the issue for me.
Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x0
This is the main problem.
If you have already migrated to androidx, you should change your layout xml
from
<android.support.design.widget.NavigationView ... />
to
<com.google.android.material.navigation.NavigationView ... />
I had the same error, I resolved it by adding app:itemTextColor="#color/a_color" to my navigation view :
<android.support.design.widget.NavigationView
android:id="#+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="left"
app:headerLayout="#layout/layout_drawer_header"
app:menu="#menu/drawer_menu"
app:itemTextColor="#color/primary"/>
You can still use android:textColorPrimary and android:textColorSecondary in your theme with this method.
Well
So I was trying to fix this error. And none worked for me. I was not able to figure out solution.
Scenario:
I was just going to made a Navigation Drawer Project inside Android Studio 2.1.2
And when I try to change the default Android icon in nav_header_main.xml I was getting some weird errors. I figured out that I was droping my PNG logo into the ...\app\src\main\res\drawable-21. When I try to put my PNG logo in ...\app\src\main\res\drawable bam! All weird errors go away.
Following are some of stack trace when I was putting PNG into drawable-21 folder:
08-17 17:29:56.237 6644-6678/myAppName E/dalvikvm: Could not find class 'android.util.ArrayMap', referenced from method com.android.tools.fd.runtime.Restarter.getActivities
08-17 17:30:01.674 6644-6644/myAppName E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{myAppName.MainActivity}: android.view.InflateException: Binary XML file line #16: Error inflating class <unknown>
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2372)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2424)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3956)
at android.app.ActivityThread.access$700(ActivityThread.java:169)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1394)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5433)
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:924)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:691)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #16: Error inflating class <unknown>
at android.view.LayoutInflater.createView(LayoutInflater.java:613)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at edu.uswat.fwd82.findmedoc.MainActivity.onCreate(MainActivity.java:22)
at android.app.Activity.performCreate(Activity.java:5179)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1146)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2336)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2424)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3956)
at android.app.ActivityThread.access$700(ActivityThread.java:169)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1394)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5433)
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:924)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:691)
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:587)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at edu.uswat.fwd82.findmedoc.MainActivity.onCreate(MainActivity.java:22)
at android.app.Activity.performCreate(Activity.java:5179)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1146)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2336)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2424)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3956)
at android.app.ActivityThread.access$700(ActivityThread.java:169)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1394)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5433)
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:924)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:691)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #14: Error inflating class ImageView
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
at android.support.design.internal.NavigationMenuPresenter.inflateHeaderView(NavigationMenuPresenter.java:189)
at android.support.design.widget.NavigationView.inflateHeaderView(NavigationView.java:262)
at android.support.design.widget.NavigationView.<init>(NavigationView.java:173)
at android.support.design.widget.NavigationView.<init>(NavigationView.java:95)
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
at android.view.LayoutInflater.createView(LayoutInflater.java:587)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at edu.uswat.fwd82.findmedoc.MainActivity.onCreate(MainActivity.java:22)
at android.app.Activity.performCreate(Activity.java:5179)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1146)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2336)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2424)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3956)
at android.app.ActivityThread.access$700(ActivityThread.java:169)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1394)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5433)
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:924)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:691)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.content.res.ResourcesEx.getThemeDrawable(ResourcesEx.java:459)
at android.content.res.ResourcesEx.loadDrawable(ResourcesEx.java:435)
at android.content.res.TypedArray.getDrawable(TypedArray.java:609)
at android.widget.ImageView.<init>(ImageView.java:120)
at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:57)
at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:53)
at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:106)
at android.support.v7.app.AppCompatDelegateImplV7.createView(AppCompatDelegateImplV7.java:980)
at android.support.v7.app.AppCompatDelegateImplV7.onCreateView(AppCompatDelegateImplV7.java:1039)
at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:
As you can see the above Stack Trace include:
android.support.design.widget.NavigationView.inflateHeaderView(NavigationView.java:262)
at android.support.design.widget.NavigationView.(NavigationView.java:173)
at android.support.design.widget.NavigationView.(NavigationView.java:95)
I solved it downgrading in gradle from
compile 'com.android.support:design:23.1.0'
to
compile 'com.android.support:design:23.0.1'
It seems like I always get problems when I update any component of Android Studio. Getting tired of it.
None of the above fixes worked for me.
What worked for me was changing
<item name="android:textColorSecondary">#FFFFFF</item>
to
<item name="android:textColorSecondary">#color/colorWhite</item>
You obviously need to add colorWhite to your colors.xml
Following below steps will surely remove this error.
Find the widget causing the error.
Go the layout file where that widget is declared.
Check for all the resources (drawables etc.) used in that file.
Then make sure that resource is there in all versions of drawables (drawable-v21,drawable etc.)
Cheers!!
As Parag Naik correctly mentions (and Lạng Hoàng expands on), the problem arises when setting textColorPrimary to something other than a color state list. So you could set textColorPrimary as a state list. There is an issue in the android bug tracker about colorPrimary being a state list with only one color: https://code.google.com/p/android/issues/detail?id=172353
So for your theme in styles.xml:
<style name="Base.Theme.Hopster" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/primary</item>
<item name="colorPrimaryDark">#color/primary_dark</item>
<item name="colorAccent">#color/accent</item>
<item name="android:textColorPrimary">#color/primary_color_statelist</item>
</style>
And the actual primary_color_statelist.xml file:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- This is used when the Navigation Item is checked -->
<item android:color="#color/primary_text_selected" android:state_checked="true" />
<!-- This is the default text color -->
<item android:color="#color/primary_text" />
</selector>
In my case, I had the same error when I run the app in kitkat API 19 version device. I figured out the problem; I had some drawable resources which was in the drawable-v21 directory (Which is used for versions from API 21 Lollipop). I just put the same resources in the "Drawable" folder to work with the version below API 21. It works. You can put it on the corresponding directory
For me, I encountered this error many times,
Error inflating class android.support.design.widget.NavigationView #28 and #29
The solution that works for me is that you must match your support design library and your support appcompat library.
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
For me they must match. :) It works for me!
Just for who still get to this issue. I got to the same problem but all the solutions here is not work for me.
Just take alook on NavigationView class with cue from logcat, i found the issue come form this line of code:
itemTextColor = this.createDefaultColorStateList(16842806);
So, it seem related to itemTextColor as Aenur56 mentioned. So i tried with Aenur56's solution but it doesn't work.
Take a look on the line of code above, i notice that there is ColorStateList. So i create one then set for itemTextColor then it work.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#00FF00" android:state_checked="true" />
<item android:color="#000000" />
</selector>
Hope it help!
None of the solutions above helped me. I found this on some other webpage and it worked for me - "In project.properties of design library set target to 21 or highest available, then clean design lib, appcomapt and your project and enjoy"
Hope it will help!
Make sure your drawables for the menu are in the correct folder, mine where put in drawablev21 hence this caused a nullpointer to the resource.
Fixed it buy moving my icons to drawables folder
Generic way of solving issues like Error inflating class X:
check cause stack trace and there should be Resources$NotFoundException like this:
Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f0b0066
at android.content.res.ResourcesImpl.getValue(ResourcesImpl.java:190)
at android.content.res.Resources.loadXmlResourceParser(Resources.java:2094)
at android.content.res.Resources.getLayout(Resources.java:1111)
at android.view.LayoutInflater.inflate(LayoutInflater.java:424)
Resource ID is actually exact integer from generated R.java file.
Find 0x7f0b0066 which will point to the xml file which could not be inflated(found).
In most cases that's because directory layout doesn't have this resource, but some layout-XXX has.
And solution is to put resource into all required directories. Or just at least into layout.
In my case i had
<android.support.design.widget.NavigationView...
and in app config:
implementation 'com.google.android.material:material:1.1.0'
implementation 'androidx.appcompat:appcompat:1.1.0'
So I changed the XML to:
com.google.android.material.navigation.NavigationView...
I had the same problem after upgrading the support library and none of the answers worked for me. Finally I solved downloading latest version of build tools and upgrading in build.gradle to buildToolsVersion "23.0.1"
One thing to double-check is that you have applied your theme correctly in your AndroidManifest.xml file. In my case, I had omitted the android:theme attribute. E.g:
<application ... android:theme="#style/Your.Theme.Here" ... >
BETTER I UPGRADED com.android.support:appcompat-v7:23.1.0
as #Ton said downgrade compile 'com.android.support:design:23.1.0'
BUT Why not to upgrade com.android.support:appcompat-v7:23.1.1
Final upgraded worked for me enjoy
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
So why to use older library. I think is now better to use both design and compact upgraded.
I was also having this same issue, after looking nearly 3 hours I find out that the problem was in my drawable_menu.xml file, it was wrongly written :D
This error can be caused due to reasons as mentioned below.
This problem will likely occur when the version of your appcompat library and design support library doesn't match.
Example of matching condition
compile 'com.android.support:appcompat-v7:24.2.0' // appcompat library
compile 'com.android.support:design:24.2.0' //design support library
If your theme file in styles have only these two,
<item name="colorPrimary">#4A0958</item>
<item name="colorPrimaryDark">#4A0958</item>
then add ColorAccent too. It should look somewhat like this.
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#4A0958</item>
<item name="colorPrimaryDark">#4A0958</item>
<item name="colorAccent">#4A0958</item>
</style>
It's weird but clean project and rebuild project worked for me.
I found solution...
if you used app:srcCompat change to android:src
this solution worked for me :)
I was facing this error in Xamarin. This was due to some files that were present in drawable-v21 folder. So I copied those files (probably icon files) to the drawable folder and the error was gone.
I am using this as a tutorial to implement the Material Theme to an existing app on pre Android 5.0 devices. My problem is that I am getting a Null Pointer Exception every-time I call getActionBar().something or getSupportActionBar().something.
Here is all I have done to implement the Material Theme.
In values/styles.xml
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppTheme" parent="AppTheme.Base"/>
<style name="AppTheme.Base" parent="Theme.AppCompat.Light">
<item name="colorPrimary">#1A7E99</item>
<item name="colorPrimaryDark">#16657A</item>
<item name="android:windowNoTitle">true</item>
<item name="windowActionBar">false</item>
</style>
<style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle">
<item name="spinBars">true</item>
<item name="color">#android:color/white</item>
</style>
<!-- View pager progress indecator theme -->
<style name="StyledIndicators" parent="AppBaseTheme">
<item name="vpiCirclePageIndicatorStyle">#style/CustomCirclePageIndicator</item>
</style>
<style name="CustomCirclePageIndicator">
<item name="fillColor">#ffffff</item>
<item name ="strokeWidth">2dp</item>
<item name ="strokeColor">#cfd3d4</item>
<item name="radius">8dp</item>
<item name="centered">true</item>
</style>
<style name="Widget"></style>
<style name="Widget.FloatingHintEditText" parent="#android:style/Widget.EditText">
<item name="android:paddingTop">0dp</item>
</style>
In my values-v21/styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="AppTheme.Base">
<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>
</style>
</resources>
As I have set the windowActionBar to false, I have a toolbar layout.
toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"/>
And finally on to the MainActivity.java
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
setSupportActionBar(toolbar);
}
getSupportActionBar().setDisplayHomeAsUpEnabled(true); // NULL POINTER EXCEPTION here
getSupportActionBar().setHomeButtonEnabled(true);
// rest of my code
}
}
I am adding my theme in the Manifest:
android:theme="#style/AppTheme"
My Logcat:
11-11 12:40:54.798: E/ResourceType(32738): Style contains key with bad entry: 0x01010479
11-11 12:40:55.349: E/AndroidRuntime(32738): FATAL EXCEPTION: main
11-11 12:40:55.349: E/AndroidRuntime(32738): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.driverdesignstudio.drvr/com.driverdesignstudio.drvr.MainActivity}: java.lang.NullPointerException
11-11 12:40:55.349: E/AndroidRuntime(32738): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2343)
11-11 12:40:55.349: E/AndroidRuntime(32738): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
11-11 12:40:55.349: E/AndroidRuntime(32738): at android.app.ActivityThread.access$600(ActivityThread.java:162)
11-11 12:40:55.349: E/AndroidRuntime(32738): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
11-11 12:40:55.349: E/AndroidRuntime(32738): at android.os.Handler.dispatchMessage(Handler.java:107)
11-11 12:40:55.349: E/AndroidRuntime(32738): at android.os.Looper.loop(Looper.java:194)
11-11 12:40:55.349: E/AndroidRuntime(32738): at android.app.ActivityThread.main(ActivityThread.java:5371)
11-11 12:40:55.349: E/AndroidRuntime(32738): at java.lang.reflect.Method.invokeNative(Native Method)
11-11 12:40:55.349: E/AndroidRuntime(32738): at java.lang.reflect.Method.invoke(Method.java:525)
11-11 12:40:55.349: E/AndroidRuntime(32738): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
11-11 12:40:55.349: E/AndroidRuntime(32738): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
11-11 12:40:55.349: E/AndroidRuntime(32738): at dalvik.system.NativeStart.main(Native Method)
11-11 12:40:55.349: E/AndroidRuntime(32738): Caused by: java.lang.NullPointerException
11-11 12:40:55.349: E/AndroidRuntime(32738): at com.driverdesignstudio.drvr.MainActivity.onCreate(MainActivity.java:123)
11-11 12:40:55.349: E/AndroidRuntime(32738): at android.app.Activity.performCreate(Activity.java:5122)
11-11 12:40:55.349: E/AndroidRuntime(32738): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
11-11 12:40:55.349: E/AndroidRuntime(32738): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
11-11 12:40:55.349: E/AndroidRuntime(32738): ... 11 more
My questions:
Why am I getting a Null Pointer Exception for the getActionBar() when I am adding my toolbar layout to the MainActivity?
How do I add Material design theme to my app.
Cheers,
Rakshak
UPDATE: your actual problem is that you have the <android.support.v7.widget.Toolbar code in an own toolbar.xml file. this has to be in activity_main.xml and all other layouts that you want to use with an actionbar.
The Toolbar allows more customization than the default ActionBar (mostly appearance related). If you want or need to customize the actionbar, you need to set a Toolbar as ActionBar. If not. you dont need to do that and you can simply use the default ActionBar provided by the Theme (you have to remove the windowActionBar false part from your theme).
if you want to use a toolbar, you have to ensure that there is a Toolbar view in your layouts.
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimaryDark"/>
then you can use your code.
but you should also ensure that getSupportActionBar() does not return null before you use it. in case that the layout is missing the toolbar.
OLD ANSWER BELOW
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
setSupportActionBar(toolbar);
}
getSupportActionBar().setDisplayHomeAsUpEnabled(true); // NULL POINTER EXCEPTION here
getSupportActionBar().setHomeButtonEnabled(true);
you only set the toolbar if the view has been found. it seems that the view has not been found.
after the if you access the actionbar that has not been set and is therefore null.
also in your them you have <item name="windowActionBar">false</item> that disables the default actionbar.
you can do following:
set windowActionbar to true in your theme then you have the default actionbar and you wont need the Toolbar.
if you want to use your toolbar on a non default location you have to ensure that findViewById(R.id.toolbar) actually returns a toolbar (check your layouts that there is a Toolbar with that id)
or do not use getSupportActionBar without testing it for null.