嵌套片段中的Android TabHost.TabSpec.setContent(...)“找不到带有Id的视图”
我一直在寻找答案并尝试不同的解决方案,现在时间相当疲惫。我正试图设计一个带有嵌套Fragments的Android应用程序,其中包含一个包含Tabs的Fragment。当我尝试将TabSpec的内容设置为另一个片段时,会发生此问题。首先,我devided在MainActivity成两个片段由功能区这不是问题的一部分devided:嵌套片段中的Android TabHost.TabSpec.setContent(...)“找不到带有Id的视图”
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".ScientificJournal"
android:weightSum="5">
<fragment
android:layout_width="match_parent"
android:layout_height="0dp"
android:name="ba.uni.dego.scientificjournal.ArticleUser"
android:id="@+id/workspace_user"
tools:layout="@layout/article_user_fragment"
android:layout_weight="2" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:focusableInTouchMode="false">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/b_test1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button3" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button4" />
</LinearLayout>
<fragment
android:layout_width="match_parent"
android:layout_height="0dp"
android:name="ba.uni.dego.scientificjournal.ArticleEditor"
android:id="@+id/workspace_editor"
tools:layout="@layout/article_editor_fragment"
android:layout_weight="2" />
</LinearLayout>
关注发生的历史问题,我将发布顶级片段的代码。该ArticleUser级看起来是这样的:
package ba.uni.dego.scientificjournal;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TabHost;
import android.widget.TextView;
public class ArticleUser extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.article_user_fragment,
container, false);
}
}
里面的article_user_fragment.xml的我定义它应该包含其他两个片段一个TabHost:
<?xml version="1.0" encoding="utf-8"?>
<TabHost 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"
android:id="@android:id/tabhost">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:showDividers="middle">
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:name="ba.uni.dego.scientificjournal.ArticleUserRead"
android:id="@+id/f_user_read"
android:layout_gravity="center"
tools:layout="@layout/article_user_read_fragment"
android:tag="Read" />
<fragment
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:name="ba.uni.dego.scientificjournal.ArticleUserArchive"
android:id="@+id/f_user_archive"
android:layout_gravity="center"
tools:layout="@layout/article_user_archive_fragment"
android:tag="Archive" />
</FrameLayout>
</LinearLayout>
</TabHost>
到这里为止我没有得到任何问题执行我的申请。现在,当我尝试编辑ArticleUser.class中的Tab外观时,出现错误,表示在尝试设置内容时无法找到视图。
package ba.uni.dego.scientificjournal;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TabHost;
import android.widget.TextView;
public class ArticleUser extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.article_user_fragment,
container, false);
TabHost th =(TabHost) view.findViewById(android.R.id.tabhost);
TabHost.TabSpec readerSpec = th.newTabSpec("Read");
readerSpec.setIndicator("Read");
readerSpec.setContent(R.id.subfragment_read);
th.addTab(readerSpec);
return view;
}
}
的错误是: 产生的原因:了java.lang.RuntimeException:无法创建选项卡的内容,因为找不到视图ID为2131492950
我已经发现了这一点:Android Tab Views - could not create tab content because could not find view with id
这:Why do I get an error while trying to set the content of a tabspec in android?
但是,这些解决方案并没有帮助我很多。你真的会帮我在这里!
最好的问候,丹尼斯。
好吧,我终于找到了我的答案在这里:http://developer.android.com/reference/android/support/v4/app/FragmentTabHost.html
返回一个新FragmentTabHost在我ArticleUser.class,而不是凭身份证找到了解决查看我的问题。