Android:膨胀 class TextView 时出错
Android: Error Inflating class TextView
我正在开发一个 Android 应用程序,我正在尝试为该应用程序设置自定义 Header,我需要根据显示的页面动态更改标题。
我收到以下错误,我无法弄清楚问题是什么
如果我从 app_header.xml 中删除文本视图,应用程序可以正常工作。
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>
谢谢,
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
您不能将自定义标题与其他标题功能结合使用
好吧,那是因为默认情况下操作栏已经设置好,它也不允许您使用自定义标题栏。
文档显示 "each activity includes the action bar":
这样,当应用程序在 Android 3.0 或更高版本上运行时,系统会将全息主题应用到每个 activity,因此,每个 activity 都包含操作栏。
尝试使用 AppCompact 主题
我正在开发一个 Android 应用程序,我正在尝试为该应用程序设置自定义 Header,我需要根据显示的页面动态更改标题。 我收到以下错误,我无法弄清楚问题是什么
如果我从 app_header.xml 中删除文本视图,应用程序可以正常工作。
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>
谢谢,
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
您不能将自定义标题与其他标题功能结合使用 好吧,那是因为默认情况下操作栏已经设置好,它也不允许您使用自定义标题栏。
文档显示 "each activity includes the action bar":
这样,当应用程序在 Android 3.0 或更高版本上运行时,系统会将全息主题应用到每个 activity,因此,每个 activity 都包含操作栏。
尝试使用 AppCompact 主题