Home > Software engineering >  How to implement a TextView's setText in a fragment using java
How to implement a TextView's setText in a fragment using java

Time:09-30

First up, I am totally new to android and java so, a very steep learning curve for me, been at it a few days now looking at all sorts of answers both here and other places lots of tries but still no solution.

Edit: Just to be clear, I have spent at least a couple of days attempting to find the answer (researching), lots of "answers" out there if you have at least some experience in android and java.

Using Android Studio...

What I am attempting to do is set the TextView to the returned value from a Socket call, input = new BufferedReader(new InputStreamReader(socket.getInputStream()));

This call returns a value that is a correct response from a hardware device, so the socket works fine (remember, this is a learning curve so I don't need to know what I may be doing wrong, that will come later) all I want at this point in time is to set the text view with the returned value of the BufferedRead input var.

This --> TextView te = (TextView)findViewById(R.id.textView); shows as a problem "Cannot resolve method 'findViewById' in 'FirstFragment'" even though the TextView is actually in the fragment and also shows in the associated xml file.

Sorry if I have not explained this well but, I am trying.

Any advice would be helpful

package com.example.mybasicactivityapp;

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

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.navigation.fragment.NavHostFragment;

import com.example.mybasicactivityapp.databinding.FragmentFirstBinding;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.nio.CharBuffer;

public class FirstFragment extends Fragment {
    private FragmentFirstBinding binding;

    TextView te;

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

    TextView te = (TextView)findViewById(R.id.textView);

    String RequestA = "/?00044000039400!\r\n";
    
            Runnable runnable = new Runnable(){
                @Override
                public void run() {
                    try {
                        Socket socket = new Socket(ipAddress, port);
                        OutputStream out = socket.getOutputStream();
                        PrintWriter output = new PrintWriter(out, true);
                        output.println(RequestA);
                        BufferedReader input;
                        input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                        String read = input.readLine();
                        String t = read;
                        
                        te = findViewById(R.id.textView);

                    }
                    catch (IOException e)
                    {
                        e.printStackTrace();
                    }
                }
            };
            Thread thread = new Thread(runnable);
            thread.start();

        binding = FragmentFirstBinding.inflate(inflater, container, false);


        return binding.getRoot();

    }

    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        binding.buttonFirst.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                NavHostFragment.findNavController(FirstFragment.this)
                        .navigate(R.id.action_FirstFragment_to_SecondFragment);
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }

}

CodePudding user response:

You have to call getView() before findViewById.

Such as

TextView te = (TextView)getView().findViewById(R.id.textView);

Note that getView() only works after onCreateView finishes, so you will also need to move this to the onViewCreated method.

CodePudding user response:

You have to get the reference to the textView from inside the onViewCreated method. Do something like binding.textView in onViewCreated method to access the textView.

CodePudding user response:

You do too much unnecessary findViewById and declare TextView

just use binding.textView to get TextView

public class FirstFragment extends Fragment {
    private FragmentFirstBinding binding;

    TextView te; //unnecessary

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

    TextView te = (TextView)findViewById(R.id.textView);//unnecessary

    String RequestA = "/?00044000039400!\r\n";
    
            Runnable runnable = new Runnable(){
                @Override
                public void run() {
                    try {
                       ...
       
                        String t = read;
                        
                        te = findViewById(R.id.textView);//unnecessary
                        binding.textView.text = t //add this line
                    }
                    catch (IOException e)
                    {
                        e.printStackTrace();
                    }
                }
            };
            Thread thread = new Thread(runnable);
            thread.start();

        binding = FragmentFirstBinding.inflate(inflater, container, false);


        return binding.getRoot();

    }
}
  • Related