How to read a text file in a Fragment Android Studio

2

Good afternoon community, I wanted to ask the question, it turns out that I try to read a text file from a fragment but when I open it in the application I jump that the application stopped working, so I wanted to know if they could give me a hand .

 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view;
    view= inflater.inflate(R.layout.fragment_vocabulary,container);
    TextView voc=(TextView) view.findViewById(R.id.vocabulario);
    try {


        InputStream archivo=getResources().openRawResource(R.raw.leccion10);

        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(archivo));
        StringBuilder stringBuilder = new StringBuilder();
        String linea;
        while((linea =bufferedReader.readLine())!= null){
            stringBuilder.append(linea).append("\n");
        }
        voc.setText(stringBuilder);
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return view;
}

Try to find the solution but I'm running out of ideas, I hope your help, thanks in advance.

    
asked by Diego Lafaye A 10.01.2018 в 17:42
source

2 answers

0
setTitle("Vocabulario");
                VocabularyFragment vocab = new VocabularyFragment();
                FragmentTransaction fragmentTransaction2 = getSupportFragmentManager().beginTransaction();
                fragmentTransaction2.replace(R.id.fram, vocab,"Fragment_vocabulary");
                fragmentTransaction2.commit();
                return true;

This block of code is to enter the fragment called VocabularyFragment. Then in the fragment_vocabulary.xml I have the following:

<TextView
    android:id="@+id/vocab"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
     />

And finally in the VocabularyFragment.java in the onCreateView method I have the following:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view;
    view= inflater.inflate(R.layout.fragment_vocabulary,container);
    TextView vocab= view.findViewById(R.id.vocab);
    try {
        leer(vocab);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return view;
}

and the read method is a void method that has the following:

public void leer(TextView vocab)throws IOException{
    try {

        InputStream archivo=getResources().openRawResource(R.raw.vocabulario);
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(archivo));
        StringBuilder stringBuilder = new StringBuilder();

        String linea;
        while((linea =bufferedReader.readLine())!= null){
            stringBuilder.append(linea).append("\n");
        }
        vocab.setText(stringBuilder);
        archivo.close();
        bufferedReader.close();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}
    
answered by 11.01.2018 в 19:08
-1

The code to get the contents of the file is correct , just make sure you have the file /raw/leccion1.txt

InputStream archivo=getResources().openRawResource(R.raw.leccion1);
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(archivo));
        StringBuilder stringBuilder = new StringBuilder();
        String linea;
        while((linea =bufferedReader.readLine())!= null){
            stringBuilder.append(linea).append("\n");
        }           

I do not see you making the capture of NullPointerException therefore I think the error is here:

  voc.setText(stringBuilder);

You must ensure that the TextView called voc , actually found within the layout fragment_vocabulary.xml :

  view= inflater.inflate(R.layout.fragment_vocabulary,container);
    TextView voc=(TextView) view.findViewById(R.id.vocabulario);

If not found, the instance of TextView will have a Null value

    
answered by 11.01.2018 в 16:32