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

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

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

setContent();

先看XML布局:

android 使用Tabhost 发生could not create tab content because could 
not find view with id 错误
<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>
android 使用Tabhost 发生could not create tab content because could 
not find view with id 错误

 

代码:

android 使用Tabhost 发生could not create tab content because could 
not find view with id 错误
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);
        
    }
 
android 使用Tabhost 发生could not create tab content because could 
not find view with id 错误

 

 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 错误

上一篇:android学习日记14--网络通信


下一篇:iOS7.1 编译报错 解决方案 体会