Android - 在res / raw中的文本文件中填充Fragment中的textView

时间:2017-05-27 16:18:50

标签: android android-fragments

我正在尝试在我的Android应用程序中的选项卡式布局中的片段中填充TextView。我已经看了2 other threads但是一旦应用程序在指定的空间中运行,文件中的文本就不会出现在屏幕上。

你觉得我在这里失踪了什么?感谢

片段

import android.os.Bundle;
import android.support.annotation.Nullable;
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 Tab1_Fragment extends android.support.v4.app.Fragment {

    InputStream is;
    BufferedReader br;
    String entireFile;
    TextView text;
    View rootview;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        rootview = inflater.inflate(R.layout.tab1_fragment,container,false);

        is = getResources().openRawResource(R.raw.test1);
        br = new BufferedReader(new InputStreamReader(is));
        String line;
        entireFile = "";
        try {
            while ((line = br.readLine()) != null) {
                entireFile = entireFile + (line + "\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        TextView textView = null;
        if (textView != null) {
            text = (TextView) rootview.findViewById(R.id.textTab1);
            text.setText(entireFile);
        }

        return rootview;
    }
}

主要活动

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "Tab Main Screen";

    private SectionsPageAdapter mSectionsPageAdapter;

    private ViewPager mViewPager;

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

        mSectionsPageAdapter = new SectionsPageAdapter(getSupportFragmentManager());

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.container);
        setupViewPager(mViewPager);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(mViewPager);
    }

    private void setupViewPager(ViewPager viewPager) {
        SectionsPageAdapter adapter = new SectionsPageAdapter(getSupportFragmentManager());
        adapter.addFragment(new Tab1_Fragment(), "Tab1");
        adapter.addFragment(new Tab2_Fragment(), "Tab2");
        adapter.addFragment(new Tab3_Fragment(), "Tab3");
        adapter.addFragment(new Tab4_Fragment(), "Tab4");
        viewPager.setAdapter(adapter);
    }
}

标签XML(所有标签均相同)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:textSize="16sp"
        android:layout_marginTop="40dp"
        android:id="@+id/textTab1"/>

</RelativeLayout>

2 个答案:

答案 0 :(得分:1)

您的if不正确,textview将始终为空,if将不会执行,因此

   if (textView == null) {
        text = (TextView) rootview.findViewById(R.id.textTab1);
        text.setText(entireFile);
    }

textViewtext两者都不同,请使用text

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
    rootview = inflater.inflate(R.layout.tab1_fragment,container,false);

    is = getResources().openRawResource(R.raw.test1);
    br = new BufferedReader(new InputStreamReader(is));
    String line;
    entireFile = "";
    try {
        while ((line = br.readLine()) != null) {
            entireFile = entireFile + (line + "\n");
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    if(text==null){
        text = (TextView) rootview.findViewById(R.id.textTab1);
    }
    text.setText(entireFile);
    return rootview;
}

答案 1 :(得分:0)

您正在初始化为null

    TextView textView = null;
    if (textView != null) {
        text = (TextView) rootview.findViewById(R.id.textTab1);
        text.setText(entireFile);
    }

如果textView初始化为null,后续的if()将如何执行?

    text = (TextView) rootview.findViewById(R.id.textTab1);
    text.setText(entireFile);

更改为此,if不是必需的。