0

I have a problem here, I'm getting a NullPointerException when I try to set text inside an EditText placed in my layout_fragment.xml.

I'm using findViewById(R.id.TextView) on the fragment parent Activity in onCreate().

The activity has drawer navigation implemented and the layout_fragment.xml belongs to a the first displayed fragment of it.

Navigation.class

EditText edt_nome;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.navigation);
    Intent intent = getIntent();
    edt_nome = (EditText)findViewById(R.id.home_nome);
    edt_nome.setText("someText");
}

The code is not much and its simplified because of a confidentiality agreement.

Bryan Herbst
  • 66,602
  • 10
  • 133
  • 120
  • post `navigation.xml` – Raghunandan May 21 '14 at 15:36
  • 1
    if edtitext is in fragment why not initialize it there?? – Raghunandan May 21 '14 at 15:39
  • 2
    possibly helpful: http://stackoverflow.com/questions/23653778/nullpointerexception-accessing-views-in-oncreate - in essence the `findViewById()` is too early and the fragment view is not yet attached to the activity – laalto May 21 '14 at 15:39
  • i have an asyncTask on the navigation.class making a connection with an api that populates an ArrayList> and i cant send it to the fragment to set his components on the EditTexts, so it have to be on Navigation.class – Bruno Lorenço May 21 '14 at 15:42
  • you can send list to fragment – Raghunandan May 21 '14 at 15:45
  • @laalto the code is after this method that attaches the fragment to the activity so i guess that's not the problem, but thanks anyway ' if (savedInstanceState == null) { // on first time display view for first nav item displayView(0); }' – Bruno Lorenço May 21 '14 at 15:46
  • bundles dont accept the object ArrayList> @Raghunandan – Bruno Lorenço May 21 '14 at 15:50
  • @BrunoLorenço http://stackoverflow.com/questions/6355787/how-to-pass-arraylisthashmapstring-stringfrom-one-activity-to-another. It should work in case of fragment also – Raghunandan May 21 '14 at 15:51
  • @Raghunandan that will resolve it, but i dont know if it will work fine when i click the update button that is on the Options Menu and will re-populate the ArrayList> object, because the intent extras are only reloaded when the fragment is restarted – Bruno Lorenço May 21 '14 at 15:54
  • @BrunoLorenço design is your problem but initialize edittext in fragment – Raghunandan May 21 '14 at 15:55

3 Answers3

0

i think you are trying to access activity to early than it is created.

so move this code

    edt_nome = (EditText)findViewById(R.id.home_nome);
edt_nome.setText("someText");

to onActivityCreated,i think that should fix the issue if not please post exception details.

sai Gorantla
  • 179
  • 9
  • What if I wanted to access the EditText in Fragment from Activity, I get a NPE http://stackoverflow.com/questions/39907625/why-is-accessing-textview-of-a-fragment-inside-activity-throwing-an-error – Si8 Oct 07 '16 at 11:50
0

You just cant edit any widget like that. I suggest to handle with fragments and implement onCreateView()

:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        rootView = inflater.inflate(R.layout.FRAGMENT_LAYOUT, container, false);
        EditText et;
        et = (EditText) rootView.findViewById(R.id.EditText);
        et.setText("SomeText");
        (...)
      }

about the previous answer, take a look on this topic:

Android Fragment onCreateView vs. onActivityCreated

Community
  • 1
  • 1
PedroHawk
  • 622
  • 5
  • 19
0

If you have declared <EditText> in layout_fragment.xml then you must use setContentView(R.layout.layout_fragment); to deal with that EditText

Tayyab Hussain
  • 1,658
  • 1
  • 18
  • 21