java - TextView setText 不起作用

标签 java android android-layout android-fragments fragmentmanager

我试图将 Text 设置为 Activity 中包含的 fragment 中的 TextView 。当我使用 setText 方法设置文本时,我可以看到 textview 的 mText 类变量被分配了该值。但是,在 UI 端我看不到代码设置的文本。

我有一个像这样的 Activity 布局。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:id="@+id/meaningcontainer"
            tools:context="com.xyz.MeaningActivity"
            tools:ignore="MergeRootFrame"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
>


<TextView
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_below="@+id/meaning_toolbar"
    android:id="@+id/word_selected"
    android:textSize="@dimen/abc_text_size_title_material_toolbar"
    />

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/word_selected"
    android:id="@+id/word_meanings"
    >

    <fragment
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/frag_meaning"
        android:name="com.xyz.ui.MeaningActivity$MeaningFragment"
        tools:layout="@layout/fragment_meaning"
        />

</FrameLayout>

此布局中包含的相应 fragment 如下:

<ScrollView xmlns:tools="http://schemas.android.com/tools"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".ui.MeaningActivity$MeaningFragment"
>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:elevation="@dimen/cardview_default_elevation"
        >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            >

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="16dp"
                android:id="@+id/postype_noun"
                android:textAlignment="textStart"
                android:textSize="@dimen/abc_text_size_title_material_toolbar"
                android:layout_marginStart="@dimen/activity_horizontal_margin"
                />

        </LinearLayout>

    </android.support.v7.widget.CardView>


</RelativeLayout>

在 Activity onCreate 方法中,我尝试设置文本。这是代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_meaning);

    if (savedInstanceState == null) {
        Fragment fragment = new MeaningFragment();
        getSupportFragmentManager().beginTransaction()
                .add(R.id.word_meanings, fragment).commit();
    }
    Fragment fragment = getSupportFragmentManager().
                            findFragmentById(R.id.frag_meaning);
    View frag_view = fragment.getView();
    TextView postextView = (TextView) frag_view.       
                                    findViewById(R.id.postype_noun);
    postextView.setText("test");

}

textView posttextView 不为 null,并被赋予 test 值。但在 UI 端我看不到任何测试字符串。

最佳答案

请从您的 Activity 的 onCreate 方法中删除以下行:

if (savedInstanceState == null) {
    Fragment fragment = new MeaningFragment();
    getSupportFragmentManager().beginTransaction()
            .add(R.id.word_meanings, fragment).commit();
}

无需执行此操作,因为您的 fragment 已通过 Activity 布局中的 XML 实例化。

关于java - TextView setText 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29329196/

相关文章:

android - 带有 RecyclerView 和底部固定 EditText 的 CoordinatorLayout

java - Wicket - 每次 ajax 刷新组件时执行 javascript

android - actionNext & textMultiline 不工作

android - 在 recyclerView 的行内处理按钮单击

java - 安卓开发 : Can't generate multiple layouts dynamically from an XML template

android - 在安卓中;是应用 OnClickListener 更好还是使用 android :onClick?

java - 为什么 Google OAuth 2.0 代码 token 交换返回 "invalid_request"?

java - 如何使用Java包?

java - 如何使用外部绑定(bind)文件覆盖 JAXB 中的默认名称?

android - 无法解析 : com. android.support :support-v4:23. 0.0