学习Android一周, 这两天用TabHost的时候, 遇到2个问题:

问题1. 运行Activity的时候出现Your content must have a TabHost whose id attribute is 'android.R.id.tabhost'

添加Layout的时候,xml跟元素选择TabHost, 但是ADT没有添加id属性, 运行的时候,会提示Your content must have a TabHost whose id attribute is 'android.R.id.tabhost'错误, 需要添加android:id="@android:id/tabhost", 这样就可以了。

问题2. 运行Activity的时候出现Your TabHost must have a TabWidget whose id attribute is 'android.R.id.tabcontent'

解决方法: 修改FrameLayout添加id属性, ADT自动生成的xml文件中Id是android:id="@+id/FrameLayout01 ", 需要修改成下面的格式android:id="@android:id/tabcontent ",这个估计会困扰一大批初学者,谁会想到会修改这个地方,看到错误很容易修改成tabcontent,但是前缀不容易想到。 而且在ADT可视化编辑这个文件的时候, 界面上显示NullPointerException,这个是ADT的一个BUG。

修改后的xml如下:

<?xml version="1.0" encoding="utf-8"?>
<TabHost
android:id="@android:id/tabhost"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<LinearLayout android:id="@+id/LinearLayout01" android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical">

<TabWidget android:id="@android:id/tabs" android:layout_height="wrap_content" android:layout_width="fill_parent">
</TabWidget>
<FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent">
</FrameLayout>
</LinearLayout>
</TabHost>

注意: 如果用TabHost的话, 上面标红的三处必须是一样, 这个是Google的约定。 而且一个工程中只能有一个TabHost。

更多相关文章

  1. Android 中 ListView 常用属性合集
  2. Android文本输入框EditText的属性和方法
  3. Android(java)学习笔记88:TextView属性大全
  4. Android 部分属性学习
  5. Android Studio添加so文件并打包到APK的lib文件夹中
  6. android_开发_EditText_and_TextView属性
  7. android studio打包 so文件
  8. Android异步框架android-async-http-master的下载导入和错误处理

随机推荐

  1. MySQL系列之二 多实例配置
  2. MySQL系列之十三 MySQL的复制
  3. MySQL系列之六 用户与授权
  4. MySQL系列之开篇 MySQL关系型数据库基础
  5. MySQL系列之五 视图、存储函数、存储过程
  6. MySQL在Windows中net start mysql 启动My
  7. MySql 缓存查询原理与缓存监控和索引监控
  8. MySQL系列之八 MySQL服务器变量
  9. MySQL系列之九 mysql查询缓存及索引
  10. 解决MySQL启动报错:ERROR 2003 (HY000):