Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 文本视图设置文本不工作_Java_Android_Android Layout_Android Fragments_Fragmentmanager - Fatal编程技术网

Java 文本视图设置文本不工作

Java 文本视图设置文本不工作,java,android,android-layout,android-fragments,fragmentmanager,Java,Android,Android Layout,Android Fragments,Fragmentmanager,我试图将text设置为活动中包含的片段中的textView。当我使用setText方法设置文本时,我可以看到textview的mText类变量被赋值。但是,在UI端,我看不到代码设置的文本 我有一个这样的活动布局 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"

我试图将text设置为活动中包含的片段中的textView。当我使用setText方法设置文本时,我可以看到textview的mText类变量被赋值。但是,在UI端,我看不到代码设置的文本

我有一个这样的活动布局

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

textViewpostextView不为空,并被分配test的值。但在UI端,我看不到任何测试字符串。

请从活动的onCreate方法中删除以下行:

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

无需执行此操作,因为您的片段已通过活动布局中的XML实例化。

尝试从活动布局中删除卡片布局。

尝试改用TextView。为什么要尝试使用内部类(
android.support.v7.internal.widget.CompatTextView
)在您自己的布局中?@commonware我最初只有textview。因为我也有同样的问题,所以我和其他班级一起玩。根据您的建议,返回到textview。仍然不起作用。使用textvew类更新问题。使用层次结构视图或
uiautomatorviewer
确定
TextView
本身是否可见,等等@Nayanjyoti尝试删除卡片布局
@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");

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