Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getSupportFragmentManager().findFragmentById() returns null

I am trying to implement fragment communication in android like the one in the android guide http://developer.android.com/guide/components/fragments.html#CommunicatingWithActivity

but my application is crashing as the getSupportFragmentManager().findFragmentById() returns null. What is the issue with my implementation.

The code is given below:

The program is just to send an input text from one fragment to another fragment textView area through a button click from first fragmnet.I have an activity_main.xml and two fragment layout (two separate xml files rather than part of in activity_main.xml)

Frag1.java

public class Frag1 extends Fragment { public Frag1(){  }  buttonClickListener buttonListener;  @Override public void onAttach(Activity activity) { super.onAttach(activity); try {     buttonListener = (buttonClickListener) getActivity(); } catch (ClassCastException e) {     throw new ClassCastException(activity.toString() + " must implement OnButtonPressListener"); } }  View myFragmentView;  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,         Bundle savedInstanceState) {     myFragmentView = inflater.inflate(R.layout.frag1, container, false);       //SetValue Button     Button setValueButton = (Button) myFragmentView.findViewById(R.id.setValueButton);     setValueButton.setOnClickListener(new OnClickListener() {          @Override         public void onClick(View v) {             buttonListener.onButtonPressed("Message received");         }     });       return myFragmentView; } 

}

Frag2.java

public class Frag2 extends Fragment {  View myFragmentView;  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,         Bundle savedInstanceState) {  myFragmentView = inflater.inflate(R.layout.frag2, container, false);     return myFragmentView; }  void setMessage(String msg){     TextView txt=(TextView)myFragmentView.findViewById(R.id.textView1);     txt.setText(msg); } } 

buttonClickListener.java

public interface buttonClickListener { public void onButtonPressed(String msg); } 

MainActivity.java

public class MainActivity extends FragmentActivity implements     ActionBar.TabListener, buttonClickListener { SectionsPagerAdapter mSectionsPagerAdapter; @Override public void onButtonPressed(String msg) {     // TODO Auto-generated method stub     Frag2 fragmentObj=(Frag2) getSupportFragmentManager().findFragmentById(R.layout.frag2);       fragmentObj.setMessage(msg); } 

Please tell me where did I go wrong?

EDIT: I am using fragment creation using the template generated by Android Plug-in eclipse IDE. So the fragments are created using android.support.v4.app.Fragment

@Override     public Fragment getItem(int position) {         Fragment fragment = null;         switch(position)         {         case 0:             return new Frag1();         case 1:              return new Frag2();         }         return fragment;     } 

The codebase is kept here for reference https://skydrive.live.com/redir?resid=D37E0F56FEC9B499!259

like image 227
Kris Avatar asked Mar 20 '13 13:03

Kris


2 Answers

Try This it works for me How to put Google Maps V2 on a Fragment Using ViewPager

<fragment             android:id="@+id/map"             android:layout_width="wrap_content"             android:layout_height="match_parent"             class="com.google.android.gms.maps.SupportMapFragment" />  GoogleMap mGoogleMap = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map)).getMap(); 
like image 156
abozaid Avatar answered Oct 08 '22 15:10

abozaid


You should have added the fragment Frag2 by calling

getSupportFragmentManager().beginTransaction().add(R.id.frag2_view, new Frag2(), "tag").commit(); 

at your MainActivity, where R.id.frag2_view is a layout defined in your main_layout.

To get that Fragment, you should then call

Frag2 obj = (Frag2)getSupportFragmentManager().findFragmentById(R.id.frag2_view); 

passing the layout id you used to add the fragment in the main_layout.

Hope it helps.

EDIT:

Since you use a ViewPager, you should use R.id.pager as the ID. I just tried with your example and it worked.

Frag2 fragmentObj=(Frag2) getSupportFragmentManager().findFragmentById(R.id.pager); 

EDIT 2:

Despite it worked, I don't really think this is the correct way, since R.id.pager its from ViewPager and you can't find, let's say, frag4 or frag5.

Ignore my answer please. I'm not sure how to do that with ViewPager, sorry.

like image 35
Eduardo Herzer Avatar answered Oct 08 '22 15:10

Eduardo Herzer