首页 > 移动平台 > 详细

android 使用Tabhost 发生could not create tab content because could not find view with id 错误

时间:2014-03-15 10:11:48      阅读:516      评论:0      收藏:0      [点我收藏+]

使用Tabhost的时候经常报:could not create tab content because could not find view with id 错误。

总结一下发生错误的原因,一般的发生在

setContent();

先看XML布局:

bubuko.com,布布扣
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TabHost

        android:id="@+id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >
  
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

 

    <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </TabWidget>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </FrameLayout>
  </RelativeLayout>
</TabHost>

</RelativeLayout>
bubuko.com,布布扣

 

代码:

bubuko.com,布布扣
protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_core);
        
     
        TabHost tabHost = (TabHost) findViewById(R.id.tabhost);
        tabHost.setup();
        
      
        Intent intent = new Intent(this, TestActivity.class);
        TabSpec tab1 = tabHost.newTabSpec("A").setIndicator("A")
                .setContent(intent);
        tabHost.addTab(tab1);
        
    }
 
bubuko.com,布布扣

 

 1、ID问题

      必须为系统自带的ID  android:id="@android:id/tabcontent"

2、Tabhost没有初始化

  必须调用 tabHost.setup(); 方法初始化

3、setContent(int viewID); 这里的参数是ID,如果出入R.layout.xxxx 一定报错

4、setContent(Intent intent); 必须以下面的方式初始化Tabhost,否则报错。

  

     LocalActivityManager localActivityManager = new LocalActivityManager(this, true);
        localActivityManager.dispatchCreate(savedInstanceState);
        
        TabHost tabHost = (TabHost) findViewById(R.id.tabhost);
        tabHost.setup(localActivityManager);

 

 

 5、检查AndroidManifest.xml 配置文件有没有配置Intent的Activity

6、Intent的Activity本身存在错误,打断点看看有没有跳进来

android 使用Tabhost 发生could not create tab content because could not find view with id 错误,布布扣,bubuko.com

android 使用Tabhost 发生could not create tab content because could not find view with id 错误

原文:http://www.cnblogs.com/chenrui7/p/3601601.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!