-2

I want to change the text of my TextView element, which is defined in the corresponding QuoteFragment.class file and the related layout.xml file. You can see the code of the method in my main activity:

private void forwardToQuoteFragment(Quote quote){
    quoteFragment = QuoteFragment.newInstance(quote);
    View view = quoteFragment.getView();
    TextView tv = (TextView) view.findViewById(R.id.quoteFragmentHeader);
    tv.setText("Quote (" + quote.getId() + "): " + quote.getQuote());
    android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
    android.support.v4.app.FragmentTransaction ft = fm.beginTransaction();
    ft.replace(R.id.container, quoteFragment);
    ft.commit();
}

My debugger tells me, that the view variable is null, therefore I got an NPE. It will make no difference, if I created the view-property in my QuoteFragment.class, which you can see below:

public class QuoteFragment extends android.support.v4.app.Fragment {
    public static final String QUOTE_FRAGMENT_TAG = "QuoteFragment";
    public static final String QUOTE_ID = "quoteId";
    private View view;
    private long quoteId;

    public QuoteFragment(){
        // required
    }

    // factory to set arguments
    public static QuoteFragment newInstance(Quote quote) {
        QuoteFragment fragment = new QuoteFragment();
        Bundle args = new Bundle();
        args.putLong(QUOTE_ID, quote.getId()); 
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onAttach(Context context){
        super.onAttach(context);
        Log.i(QUOTE_FRAGMENT_TAG, "onAttach()");
    }

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        Log.i(QUOTE_FRAGMENT_TAG, "onCreate()");
        setHasOptionsMenu(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        view = inflater.inflate(R.layout.fragment_quote, container, false);
        return view;
    }

    public void setQuoteId(long id){
        this.quoteId = id;
    }

    public long getQuoteId() {
        return quoteId;
    }

    public View getView(){
       return this.view;
    }
}

What is the best way to solve this issue? What did I overlooked?

1 Answer 1

5

You can't get the View, because the View was not drawn to the screen yet: onCreateView() inside your Fragment has not been called, yet, but you are already trying to access the TextView which only will be created when onCreateView() is called.

You need to set the text inside the OnCreateView() method like so

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View view = inflater.inflate(R.layout.fragment_quote, container, false);
        TextView tv = (TextView) view.findViewById(R.id.quoteFragmentHeader);
        tv.setText("Quote (" + quote.getId() + "): " + quote.getQuote());
        return view;
    }
4
  • Thanks for your reply Steve - I will give it a try. I thought if the TextView is marked in the layout.xml it will get created?! Anyway I am not sure if your approach will solve my issue, because I got the information, which text should be printed in my MainActivity and not my Fragment. That was the reason, why I wanted to grab the TextView from the Fragment in my activity and call the corresponding setter-method.
    – Jochen
    Jan 1, 2017 at 8:30
  • 1
    The simplest way of getting your Qoute is by putting it in your Bundle as a String, which you are creating in the newInstance() method: args.putString(QUOTE_QUOTE, quote.getQoute());
    – Steve2955
    Jan 1, 2017 at 9:40
  • Or you could take a slightly more complex approce by using Parcelable and the putting the whole Qoute in the Bundle as a Parcelable
    – Steve2955
    Jan 1, 2017 at 9:49
  • Thanks a lot - the approach by giving the parameters by bundle worked out great!
    – Jochen
    Jan 1, 2017 at 11:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.