title: Getting value from Fragment into mainActivity
body: <p>I have a MainActivity. It's layout contains a fragmentContainer and an actionBar. Then I have 2 different fragments containing various EditTexts.All of them have Ids. I populate the fragmentContainer with the fragments when user clicks a button in actionBar. Everything works perfectly.</p>
  
  <p>Now...the app, is supposed to collect the contents of all EditTexts from both fragments when I push a button (in the menu of the actionBar). But it does not work. It crashes when I try to access the EditTexts directly from the activity. I understand that I should create a public method inside the code of the fragments that will return the values I am interested in. But I seem to be doing something wrong because the method is not accessible from the main (TabActivity)</p>
  
  <p>Here is my code:</p>
  
  <pre><code>public class TabActivity extends Activity {
      ActionBar.Tab tab_alocare, tab_merc;
      Fragment fragmentTab1 = new aloc_fragment();
      Fragment fragmentTab2 = new merc_fragment();
  
      @Override
        protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_tab); // this layout contains the fragmentContainer
          ...// here I do some actionBar stuff
          }
  ...
  }
  </code></pre>
  
  <p>An example of one of the fragments is bellow:</p>
  
  <pre><code>public class aloc_fragment extends Fragment {
      EditText mEditText;
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
          // Inflate the layout for this fragment
          return inflater.inflate(R.layout.fragment_aloc, container, false);
      }
      @Override
      public void onActivityCreated(Bundle savedInstanceState) {
          super.onActivityCreated(savedInstanceState);
          mEditText = (EditText) getView().findViewById(R.id.edaloc);
      }
      public String getMyText() {
          String rez = "";
          if (mEditText.getText()!=null) {
              rez = mEditText.getText().toString();
          }
          return rez;
      }
  }
  </code></pre>
  
  <p>Now... if inside the Activity code I want to access the edaloc EditText using findViewById, it crashes the app with nullException. So I then tried to access the public method of the fragment so that it would return me the value that I need. But "getMyText" method is not accessible from the TabActivity.</p>
  
  <p>So, this code does not work:</p>
  
  <pre><code>@Override
    public boolean onOptionsItemSelected(MenuItem item) {
      switch (item.getItemId()) {
          case R.id.save:
            Fragment alocfragment = (Fragment) getFragmentManager().findFragmentByTag("aloc");
            String s = alocfragment.getMyText(); // this is not compiling at all
  </code></pre>
  
  <p>I am clearly doing something wrong.
  Please advise.</p>
  
  <p>Thank you</p>