Search code examples
javaandroidxmlandroid-themeandroid-inflate

Android: Error Inflating class TextView


I am working on an Android application, I'm trying to set up a custom Header for the app and i need to change the title dynamically according to the page displayed. I'm getting the below error and i can't figure out what the problem is

If I remove the textview from app_header.xml the app works fine.

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bamm/com.bamm.MainActivity}: android.view.InflateException: Binary XML file line #12: Error inflating class TextView

MainActivity.java

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.activity_main);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.app_header);
...

app_header.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#006442"
    android:gravity="center_vertical"
    android:paddingBottom="10dp"
    android:paddingEnd="@dimen/activity_horizontal_margin"
    android:paddingStart="@dimen/activity_horizontal_margin"
    android:paddingTop="10dp">

   <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_centerVertical="true"
    android:text="@string/app_name" />

    <ImageView
        android:id="@+id/logo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true"
        android:src="@drawable/bamm_tab" />

    <ImageButton
        android:id="@+id/settings"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_centerVertical="true"
        android:background="@android:color/transparent"
        android:src="@drawable/ic_settings" />

</RelativeLayout>

bammTheme.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <style name="CustomWindowTitleBackground">
        <item name="android:background">#ffffff</item>
    </style>

    <style name="bammTheme" parent="android:Theme">
        <item name="android:windowTitleSize">50dip</item>
        <item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="android:statusBarColor">@color/colorPrimaryDark</item>
        <item name="android:colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="android:windowActionBar">false</item>
        <item name="vpiTabPageIndicatorStyle">@style/MyTabPageIndicator</item>
    </style>

    <style name="Widget.Button.Toggle" parent="android:Widget">
        <item name="android:background">@drawable/ic_toggle_bg</item>
        <item name="android:disabledAlpha">?android:attr/disabledAlpha</item>
    </style>

    <style name="toggleButton">
        <item name="android:buttonStyleToggle">@style/Widget.Button.Toggle</item>
        <item name="android:textOn"></item>
        <item name="android:textOff"></item>
    </style>

    <style name="MyTabPageIndicator" parent="Widget.TabPageIndicator">
        <item name="android:background">@drawable/custom_tab_indicator</item>
        <item name="android:paddingTop">7dp</item>
        <item name="android:paddingBottom">1dp</item>
    </style>
</resources>

Manifest.xml

<application
        android:allowBackup="true"
        android:configChanges="locale"
        android:icon="@drawable/ic_launcherkat"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/bammTheme" >

<activity
    android:name="com.bamm.MainActivity">
</activity>

Thanks,


Solution

  • The action bar is included in all activities that use the Theme.Holo theme (or one of its descendants), which is the default theme when either the targetSdkVersion or minSdkVersion attribute is set to "11" or greater.

    http://developer.android.com/guide/topics/ui/actionbar.html#Adding

    You cannot combine custom titles with other title features Well, that's because, by default action bar is setup already and it won't allow you to use a custom title bar as well.

    Documentation reveals that "each activity includes the action bar":

    This way, when the application runs on Android 3.0 or greater, the system applies the holographic theme to each activity, and thus, each activity includes the action bar.

    Try using AppCompact theme