0

I have a TextView block defined in a layout.

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/article"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
android:textSize="18sp" />

I define private attribute of a class as:

View inflatedView;

Later, in onCreateView method, it's given the value of:

inflatedView = inflater.inflate(R.layout.article_view, container, false);

When accessing the TextView field from the onCreateView method, it makes no problem, however, trying to access it from other methods, returns null reference.

Here's the code I'm using:

TextView article = (TextView) inflatedView.findViewById(R.id.article);

I managed to get it to work by setting the article as private attribute, initializing it's value in onCreateView method, then using it in all other methods, however, I can't understand what's the problem with the approach above.

EDIT:

View inflatedView;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    if (savedInstanceState != null) {
        mCurrentPosition = savedInstanceState.getInt(ARG_POSITION);
    }
    //article_view contains the article TextView
    inflatedView = inflater.inflate(R.layout.article_view, container, false);
    return inflatedView;
}

public void updateArticleView(int position) {
    TextView article = (TextView) inflatedView.findViewById(R.id.article);
    article.setText(Ipsum.Articles[position]);
    mCurrentPosition = position;
}
8
  • can you provide full code implementation of the first approach? Commented Oct 23, 2015 at 22:57
  • If the TextView is the root element in the layout, you don't need to call findViewById(...) - the call to inflate(...) will return the TextView directly. Commented Oct 23, 2015 at 23:03
  • and from where were you calling the updateArticleView() function? Commented Oct 23, 2015 at 23:10
  • @adelphus Can you explain it more thoroughly? What do I call inflate on? Commented Oct 23, 2015 at 23:13
  • 1
    @hazeiio in your code, you are already calling inflate() - just cast the resulting inflatedView variable to a TextView Commented Oct 23, 2015 at 23:16

2 Answers 2

1

So I solved the problem, by putting the TextView block inside the LinearLayout, and then calling the findViewById(R.id.article);

Managed to get the TextView both by using:

inflatedView.findViewById(R.id.article);

and

getView().findViewById(R.id.article);
Sign up to request clarification or add additional context in comments.

Comments

0

Make sure that inflatedView is not null before using this method.

2 Comments

I checked it, the ".setText()" function is never called if it's null.
because it will cause an exception.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.