当我从上一个片段视图切换到时,片段视图为空白

时间:2015-06-16 14:04:13

标签: java android android-fragments

当我在片段中填充文本视图时,它在第一个片段中显示正常。但是当我切换到另一个由另一个文本文件填充的片段时,它是空白的。

有人可以帮我理解为什么会这样吗?

这是我的第一个片段的代码:

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 ResourcesFragment extends Fragment {


    public ResourcesFragment(){

    }


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


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

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

        InputStream is = getResources().openRawResource(R.raw.resources);
        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
                //is.close();

            }
        } 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;
        return rootView;

    }
}

我在第二个片段中做同样的事情,但使用不同的源文件:

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 TaweedFragment extends Fragment {

    public TaweedFragment(){}

    @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);
        }
        else {
            text.setText("");
            text.setText(entireFile);
        }

        //return rootView;
        return rootView;
    }
}

我做错了什么??? 我尝试关闭流,但这没有用。

ironmantis7x

1 个答案:

答案 0 :(得分:0)

我的错误发生在我在主活动java文件中的其他地方设置的数组资源中。 这是固定的。

IM7X