错误二进制 XML 文件行 #2:在 android 编程中膨胀 class 时出错
Error Binary XML file line #2: Error inflating class on android programming
我有一个具有 2 种布局的 android 程序。第一个布局是登录。当登录首先开始时,我没有问题。当我尝试将 AndroidManifest 从登录更改为初始布局时,出现错误。错误就像这篇文章的标题。
在此 android 清单中,我将代码从 :
更改为
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
>
<activity
android:name=".Login"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
至:
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
>
<activity
android:name=".Splash"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
登录布局如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:orientation="vertical"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".Login">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Your total is 0"
android:textSize="45dp"
android:gravity="center"
android:id="@+id/tvDisplay"
/>
<Button
android:layout_width="250dp"
android:layout_height="100dp"
android:text="Add One"
android:gravity="center"
android:layout_gravity="center"
android:id="@+id/bAdd"
android:textSize="20dp"/>
<Button
android:layout_width="250dp"
android:layout_height="100dp"
android:text="Substract One"
android:gravity="center"
android:layout_gravity="center"
android:id="@+id/bSub"
android:textSize="20dp"/>
这里是启动画面的布局:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:background="@drawable/sertifikat"
tools:context=".Splash"></LinearLayout>
我的代码结构:
登录logcat:
这里splash.java
如何修复我的代码?
很明显,你在最后的启动布局中丢失了一个字符'/'。
您在 splash.xml 中缺少 LinearLayout 的结束标记。将 </LinearLayout>
添加到文件末尾。您可能还想考虑使用带 android:scaleType="centerCrop"
的 ImageView 而不是使用带背景的 LinearLayout。使用 android:background
将拉伸以适应屏幕的大小,这可以在不同的设备上提供不同的纵横比,而 ImageView 将缩放它以保持纵横比(取决于 scaleType)。
此外,LinearLayout 不适用于单个图像,因为它是其他视图的容器。
为了获得更好的性能,我建议您遵循以下步骤:
http://www.cutehacks.com/blog/2014/01/13/splash-screens
如果您使用高端设备,您不会看到很大的不同,但对于普通设备,您会看到巨大的改进
嘿你错过了一些小东西
这是您的代码中的一个小示例
on Github that i wrote
这是 运行 您的代码示例
结束标签除外
<intent-filter>
应该只有一个
很可能问题出在你的@drawable
看看这些教程,它们都很棒
http://www.androidhive.info/2013/07/how-to-implement-android-splash-screen-2/
我有一个具有 2 种布局的 android 程序。第一个布局是登录。当登录首先开始时,我没有问题。当我尝试将 AndroidManifest 从登录更改为初始布局时,出现错误。错误就像这篇文章的标题。
在此 android 清单中,我将代码从 :
更改为<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
>
<activity
android:name=".Login"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
至:
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
>
<activity
android:name=".Splash"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
登录布局如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:orientation="vertical"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".Login">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Your total is 0"
android:textSize="45dp"
android:gravity="center"
android:id="@+id/tvDisplay"
/>
<Button
android:layout_width="250dp"
android:layout_height="100dp"
android:text="Add One"
android:gravity="center"
android:layout_gravity="center"
android:id="@+id/bAdd"
android:textSize="20dp"/>
<Button
android:layout_width="250dp"
android:layout_height="100dp"
android:text="Substract One"
android:gravity="center"
android:layout_gravity="center"
android:id="@+id/bSub"
android:textSize="20dp"/>
这里是启动画面的布局:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:background="@drawable/sertifikat"
tools:context=".Splash"></LinearLayout>
我的代码结构:
登录logcat:
这里splash.java
如何修复我的代码?
很明显,你在最后的启动布局中丢失了一个字符'/'。
您在 splash.xml 中缺少 LinearLayout 的结束标记。将 </LinearLayout>
添加到文件末尾。您可能还想考虑使用带 android:scaleType="centerCrop"
的 ImageView 而不是使用带背景的 LinearLayout。使用 android:background
将拉伸以适应屏幕的大小,这可以在不同的设备上提供不同的纵横比,而 ImageView 将缩放它以保持纵横比(取决于 scaleType)。
此外,LinearLayout 不适用于单个图像,因为它是其他视图的容器。
为了获得更好的性能,我建议您遵循以下步骤: http://www.cutehacks.com/blog/2014/01/13/splash-screens
如果您使用高端设备,您不会看到很大的不同,但对于普通设备,您会看到巨大的改进
嘿你错过了一些小东西
这是您的代码中的一个小示例 on Github that i wrote
这是 运行 您的代码示例
结束标签除外
<intent-filter>
应该只有一个
很可能问题出在你的@drawable
看看这些教程,它们都很棒
http://www.androidhive.info/2013/07/how-to-implement-android-splash-screen-2/