0

Our app has several fragments. In one of them user fills several TextEdit fields. When he finishes he presses a button in the ActionBar to save the data. The Action just calls a private method named "saveData" that fetches all data from the fields and submit it to our server.

We have many stack traces from our users showing that getView() returns null in method saveData, but for just a small part of them. For most of them there is no problem at all. We cant reproduce the problem and we cant understand what might be causing it. The code is pretty simple:

View vw = this.getView();

EditText et;

et = (EditText)vw.findViewById(R.id.editEmail);
String email = et.getText().toString().trim();

et = (EditText)vw.findViewById(R.id.editPassword);
String password = et.getText().toString().trim();

The action is added in osResume, see below:

public void onResume() {
    super.onResume();

    MainActivity act = (MainActivity)this.getActivity();

    act.bar.removeAllActions(); 
    act.bar.addAction(new SaveAction()); 
}

Any ideas? How can we reproduce it?

Eduardo Mauro
  • 1,515
  • 1
  • 26
  • 38
  • 2
    need more code with context to *guess* at an answer. Is this in a fragment activity? How us more – Blundell Jul 06 '13 at 21:07
  • Blundell, the fragment was added to a fragment activity. There is an ActionBar with a button. The button when pressed just calls a method which starts with the lines above. So the fragment is visible when the problem happens. – Eduardo Mauro Jul 06 '13 at 21:49
  • maybe you call the getView before the onCreateView is called? – android developer Jul 06 '13 at 23:27
  • 1
    *fyi* I don't use `getView` and just hold a field myself to the root view before I return it from `createView` – Blundell Jul 06 '13 at 23:31
  • 1
    Sounds like, because the button is in the actionBar it can be pressed even when the fragment isn't attached the to Activity. So you'd need checks around this. Show us the code where you dd the button to ActionBar, i.e. where do you do it? – Blundell Jul 06 '13 at 23:33
  • I cant see how the method can be called before the onCreateView since the user commanded the operation. – Eduardo Mauro Jul 06 '13 at 23:33
  • Nice idea, Blundell, about storing the view. Do you think it may have have any side effect? I add the button in the onResume event. I added the code to the question. – Eduardo Mauro Jul 06 '13 at 23:35
  • @EduardoMauro so you mean you've overridden the getView method, and returned a field that was stored within the onCreateView ? – android developer Jul 07 '13 at 05:22
  • @Blundell you pointed to the right direction. Add it as an answer and I will accept it. – Eduardo Mauro Jul 16 '13 at 18:25

1 Answers1

0

Can you tell from your logs whether the problem is always for the same users / devices ?

I see from the code that you have submitted that the view is in the same fragment - is that actually the case ?

It's POSSIBLE that a fragment no longer in view can have their view destroyed in order to free up resources. e.g.

getView() returns null

If I suspected that this might be the case then I would attempt to recreate the problem on a phone / tablet / emulator with limited resources.

Good luck !

Community
  • 1
  • 1
IanB
  • 3,489
  • 1
  • 20
  • 24
  • Thanks, IanB. Yes, the view is in the same fragment. What I don't understand is how the view can be destroyed if the user just pressed the Action button. I can't use an emulator since the app requires Google Play services. – Eduardo Mauro Jul 06 '13 at 21:47
  • If it's in the same fragment then I don't see how my suggestion could be correct. – IanB Jul 06 '13 at 23:41
  • I need the Map Service that comes with the Google Play Services. I couldn't install it. – Eduardo Mauro Jul 06 '13 at 23:44
  • I deleted that comment - I was going off at a tangent! However, I still think it is important that you try to find a way of recreating this. Have you checked out the answer for this ?http://stackoverflow.com/questions/9488595/getview-returning-null-when-fragment-has-been-created-from-an-activity – IanB Jul 07 '13 at 10:33