zoukankan      html  css  js  c++  java
  • 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布局:

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

    代码:

    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);
            
        }
     

     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-为应用单元测试
    android手机拨号器实现
    android模拟器使用
    android开发环境搭建
    C语言之关键字
    linux shell脚本基础-----3
    linux shell脚本基础-----2
    linux shell脚本基础-----1
    Android学习计划
    MySql 绿色版配置
  • 原文地址:https://www.cnblogs.com/chenrui7/p/3601601.html
Copyright © 2011-2022 走看看