繁体   English   中英

文本未出现在Android导航抽屉应用程序的“片段”视图中

[英]Text not appearing in Fragment view in Android Navigation drawer app

我有一个正在使用的Android应用程序,它具有一个导航抽屉。 我有抽屉的内容,然后开始了一个Fragment视图。 代码可以编译,但是片段是空的视图(视图中没有显示文本)。

有人可以帮我弄清楚我做错了什么吗?

这是我的代码(来自该片段的java类文件):

package com.example.ironmantis7x.infonewmuslim;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;


/**
 *
 */
public class TawheedFragment extends Fragment {

    public TawheedFragment(){}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_tawheed, container, false);

        TextView text = (TextView) rootView.findViewById(R.id.txttawheed);
        //text.setText("your text!");

        InputStream is = getResources().openRawResource(R.raw.tawheed);
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line;
        String entireFile = "";
        try {
            while((line = br.readLine()) != null) { // <--------- place readLine() inside loop
                entireFile += (line + "\n"); // <---------- add each line to entireFile

            }
        }

        catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        //TextView text = null;
        //text.setText(entireFile); // <------- assign entireFile to TextView
        //assert text != null;
        if (text != null) {
            text.setText(entireFile);
        }
        return rootView;
    }

}

这是片段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">

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <TextView
            android:id="@+id/txttawheed"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:textSize="16dp"
            android:gravity="fill_horizontal|top|left"
            android:scrollbars = "vertical"/>

    </ScrollView>

</LinearLayout>

谢谢。

Ironmantis7x

我没有将片段正确链接到主要活动。 我修好了它。 感谢您的帮助,并让我发帖!

Ironmantis7x

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM