Android:FarmingTabHost-未找到id xxx的选项卡内容框架布局
我陷入了Android:FarmingTabHost-未找到id xxx的选项卡内容框架布局,android,fragment-tab-host,Android,Fragment Tab Host,我陷入了“在XXX中找不到选项卡内容框架布局”错误。请帮忙 我的代码如下: main活动 package com.example.demo; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentTabHost; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import a
“在XXX中找不到选项卡内容框架布局”
错误。请帮忙
我的代码如下:
main活动
package com.example.demo;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import com.example.demo.fragments.StateFragment;
public class MainActivity extends AppCompatActivity {
private FragmentTabHost mTabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setSupportActionBar((Toolbar) findViewById(R.id.toolBar));
initTabHost();
}
private void initTabHost(){
mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("Simple"),
StateFragment.class, null);
}
}
活动\u main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Title" />
</android.support.v7.widget.Toolbar>
<FrameLayout
android:id="@+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<android.support.v4.app.FragmentTabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.v4.app.FragmentTabHost>
</LinearLayout>
我在谷歌找到了这个。它是通过两个版本的v4库
实现的。所以我检查了我的
build.gradle:
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:support-v4:22.2.0'
compile 'com.android.support:appcompat-v7:22.2.0'
}
我的libs目录上什么也没有。因此,没有两个版本的v4库发生冲突
调试后,我发现在MainActivity中可以找到id为“realtabcontent”的FrameLayout。当代码运行到步骤中时:
private void ensureContent() {
if (mRealTabContent == null) {
mRealTabContent = (FrameLayout)findViewById(mContainerId);
if (mRealTabContent == null) {
throw new IllegalStateException(
"No tab content FrameLayout found for id " + mContainerId);
}
}
}
在FragmentTabHost.java
中,方法findViewById(mContainerId)
返回null。太令人困惑了
因此,请帮帮我~Orz您的问题是,您正在从android资源@android:id/tabhost引用您的tabhost,并从您的XML中引用带有@+id/realtabcontent的framelayout,因此系统无法膨胀它们 正确的方法:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Title" />
</android.support.v7.widget.Toolbar>
<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_below="@id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"/>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
当我从Eclipse迁移我的样本时,我也遇到了同样的问题 到安卓工作室 我通过以下方式解决了这个问题:
如果我帮你解决了你的问题,请将答案标记为已更正。谢谢你的帮助。但我还是很困惑。我根据页面编码:和其他网站,如:。我发现他们在FragmentTabHost之外放置了一个FrameLayout,并将其命名为id“readtabcontent”,并且成功了。请注意,您的代码与您引用的页面中的代码不同。在本教程中,他们使用带有TabWidget的FragmentTabHost,然后使用上面的FrameLayout来显示片段的内容。在您的代码中,一方面使用FragmentTabHost中的TabWidget和FrameLayout,另一方面使用FrameLayout,这是错误的。检查您的代码和示例代码,并查看差异。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Title" />
</android.support.v7.widget.Toolbar>
<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_below="@id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"/>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);
mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("Simple"),
StateFragment.class, null);