1

So I have created a Fragment for my app. In the fragment, I have an EditText field created through the xml file (whose id is myText). Now in my Java class of this fragment, I am creating a reference to this text field by using the command:

EditText myText = (EditText) getView().findViewById(R.id.myText);

However, I get a warning on this statement saying:

Method invocation `getView().findViewById(R.id.myText)' may produce 'java.lang.NullPointerException'.

Also, when I run the app, it terminates prematurely due to the same NullPointerException. I did the exact same thing except without using a fragment (that is, I created an EditText field in the xml file of my main activity) and created a reference to it in the Java file using the command:

EditText myText = (EditText) findViewById(R.id.myText);

and it works perfectly.

So my question is, why does this work in the second case and not in the first? How do I make it work in the first case?

Damian Kozlak
  • 7,065
  • 10
  • 45
  • 51
  • 1
    This question is **not** duplicate of *What is a Null Pointer Exception, and how do I fix it?*. It's pure Android question about getting `View`, not about variable, which has not been initialized. **I'm voting to reopen it.** – Damian Kozlak Jan 08 '16 at 20:18
  • @DamianKozlak, look here: http://stackoverflow.com/questions/6495898/findviewbyid-in-fragment – piotrek1543 Jan 08 '16 at 20:31
  • and http://stackoverflow.com/questions/12536125/findviewbyid-within-fragment – piotrek1543 Jan 08 '16 at 20:31
  • Do still you think that it's not duplicate? ;-) – piotrek1543 Jan 08 '16 at 20:32
  • 1
    @piotrek1543 Please read carefuly what I wrote. By marking question as duplicate you must link question, where it has been already answered. Question *What is a Null Pointer Exception, and how do I fix it?* marked by Hrundi **does not contain answer**, which answers James Bond question direcly. Links provided by you are better and these may be linked at the top of this post, under *This question already has an answer here:* text :) – Damian Kozlak Jan 08 '16 at 20:37
  • @piotrek1543 Also, James Bond was trying to do something on his own - by using `getView()`, so he get `NullPointerException` for the reason that he was trying to find component in wrong view, so he (probably) needed explanation why `getView()` is wrong in this situation too. – Damian Kozlak Jan 08 '16 at 20:46
  • @DamianKozlak the OP is not wondering why the NPE occurs, which is what they should do first. The marked duplicate will help them do that. – njzk2 Jan 08 '16 at 20:47

2 Answers2

3

Take a look at this guide from the official Android documentation.

When you create a fragment, you inflate the fragment's view using the LayoutInflater provided in onCreateView

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

To access your views in this layout, you can call findViewById(R.id.yourId); on the view that was returned by inflater.inflate

All together it should look something like this

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.yourLayout, container, false);
    EditText myText = (EditText) view.findViewById(R.id.myText);

    ...
    return view;
}
Andrew Brooke
  • 12,073
  • 8
  • 39
  • 55
2

It's because using getView() you may not get view of inflated layout, which contains desired component. Also there is an option that you are trying to getView(), while view has not been yet created, so this will result in NPE too.

You should initialize your view, where you are sure that this View contains component which you are looking for. In Fragment it will be

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,    Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_layout, container, false);
    EditText myText = (EditText) v.findViewById(R.id.myText);
    return v;
}
Damian Kozlak
  • 7,065
  • 10
  • 45
  • 51