Android 将textview设置为片段中的其他xml

Android 将textview设置为片段中的其他xml,android,android-layout,android-fragments,android-tabhost,Android,Android Layout,Android Fragments,Android Tabhost,我正在做一个android项目,它使用一个Tabhost链接到3个片段,需要它们之间进行一些通信。每个选项卡将包含另一个xml文件。我将不同sml文件中的不同元素链接到.java文件。但它似乎不起作用。代码如下: 在Main.xml中 <TabHost> <LinearLayout> <include android:id="@+id/fragmentreview" layout="@layout/fragentreview"/> </L

我正在做一个android项目,它使用一个Tabhost链接到3个片段,需要它们之间进行一些通信。每个选项卡将包含另一个xml文件。我将不同sml文件中的不同元素链接到
.java
文件。但它似乎不起作用。代码如下:

在Main.xml中

<TabHost>
 <LinearLayout>
  <include 
 android:id="@+id/fragmentreview" 
 layout="@layout/fragentreview"/>
 </LinearLayout>` 

当我启动应用程序textViewinfragR时,它应该显示
Main Car integer=
,但它仍然在新的xml评论中显示
文本。我的代码有问题吗?

您将文本设置在错误的位置。尝试将其设置为onActivityCreated

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    txtMsgR = (TextView) view.findViewById(R.id.textViewinfragR);
    txtMsgR.setText("Main Car integer = ");
}
这应该行得通。充气完成后将调用onViewCreated方法


onCreateView->onViewCreated->onActivityCreated

嗨,我稍微编辑了一下我的答案。使用为OnViewCreatedal提供的视图对象,因此请确保正确使用TabHost。[link]()这里是一个TabHost使用活动的示例,但是使用片段是可以的。
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    txtMsgR = (TextView) view.findViewById(R.id.textViewinfragR);
    txtMsgR.setText("Main Car integer = ");
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    txtMsgR = (TextView) view.findViewById(R.id.textViewinfragR);
    txtMsgR.setText("Main Car integer = ");
}