Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to Pass Data between Fragments?
    primarykey
    data
    text
    <p>For all those who are downvoting and voting for this question to be closed thinking it has something to do with the scope of textview.....then have a look,it has nothing to do with the scope of textview</p> <p>Unable to pass data between fragments,the app crashes,i don't know what i am doing wrong...i followed this link <a href="http://manishkpr.webheavens.com/android-passing-data-between-fragments/" rel="nofollow">http://manishkpr.webheavens.com/android-passing-data-between-fragments/</a></p> <p>here is the code i am using Fragment A(this fragment is passing the data)</p> <pre><code>public class FragmentSongs extends Fragment implements Serializable,OnFragmentCreatedListener { ListView SngList; ArrayList&lt;SongDetails&gt; Songinfo=null; AdapterView.AdapterContextMenuInfo info; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { ViewGroup view = (ViewGroup) inflater.inflate(R.layout.fragment_song, null); SngList = (ListView) view.findViewById(R.id.SongList); registerForContextMenu(SngList); //File f=new File(Environment.getExternalStorageDirectory()+"/Music"); File f=new File("/system/"); int j=0;int i=0; getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); Songinfo = getSongsFromDirectory(f); //sorting done below if (Songinfo.size()&gt;0) { for( j=0; j&lt;Songinfo.size();j++) { for ( i=j+1 ; i&lt;Songinfo.size(); i++) { SongDetails a=Songinfo.get(i); SongDetails b=Songinfo.get(j); if(a.getSong().toLowerCase().compareTo(b.getSong().toLowerCase())&lt;0) { Songinfo.set(i,b ); Songinfo.set(j,a); } } } SngList.setAdapter(new CustomAdapter(Songinfo)); return view; } else return null; } OnFragmentCreatedListener listener; @Override public void onAttach(Activity activity) { super.onAttach(activity); try { listener = (OnFragmentCreatedListener) getActivity(); listener.onFragmentCreated(Songinfo); } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement onFragmentCreated"); } } </code></pre> <p>fragment B(the receiver fragment )</p> <pre><code> public class FragmentArtists extends Fragment { ArrayList&lt;SongDetails&gt; songinfo2; ViewGroup view; int s=5;//TextView text; public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {Context context; LayoutInflater lf = getActivity().getLayoutInflater(); view = (ViewGroup) inflater.inflate(R.layout.fragment_artist, null); // view = lf.inflate(R.layout.test,container, false); // view = inflater.inflate(R.layout.test,container, false); s=s+5; // text=(TextView)view.findViewById(R.id.tt);when i use this,it works fine but i want to do it in the other method //text.setText("makzzz");when i use this,it works fine but i want to do it in the other method return view; } void setSongList(ArrayList&lt;SongDetails&gt; songinfo) { //TextView text=(TextView)this.getView().findViewById(R.id.tt);//when i used this view was not global TextView text=(TextView)view.findViewById(R.id.tt);//when i used this,view was global; //TextView text=(TextView)view.findViewById(R.id.tt); // text=(TextView)super.getView().findViewById(R.id.tt); text.setText("makzzz"); </code></pre> <p>the method in holder activity (this activity holds the fragments)</p> <pre><code>protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.mymusic); fragments = new Vector&lt;Fragment&gt;(); fragments.add(Fragment.instantiate(this, FragmentSongs.class.getName())); fragments.add(Fragment.instantiate(this, FragmentArtists.class.getName())); fragments.add(Fragment.instantiate(this, FragmentPlaylists.class.getName())); fragments.add(Fragment.instantiate(this, FragmentAlbums.class.getName())); ViewPagerAdapter adapter = new ViewPagerAdapter(super.getSupportFragmentManager(), fragments); ViewPager pager = (ViewPager)super.findViewById(R.id.viewpager); pager.setAdapter(adapter); pager.setOffscreenPageLimit(4); pager.setCurrentItem(0); } public void onFragmentCreated(ArrayList&lt;SongDetails&gt; msg) { FragmentArtists Obj=(FragmentArtists) fragments.get(1); Obj.setSongList(msg); } } </code></pre> <p>the callback class:</p> <pre><code>package sourcecode.jazzplayer; import java.util.ArrayList; public interface OnFragmentCreatedListener { public void onFragmentCreated(ArrayList&lt;SongDetails&gt; msg); } </code></pre> <p>here is the logcat</p> <pre><code> 09-19 23:50:46.851: E/AndroidRuntime(2125): java.lang.NullPointerException 09-19 23:50:46.851: E/AndroidRuntime(2125): at sourcecode.jazzplayer.FragmentArtists.setSongList(FragmentArtists.java:55) 09-19 23:50:46.851: E/AndroidRuntime(2125): at sourcecode.jazzplayer.MyMusic.onFragmentCreated(MyMusic.java:45) 09-19 23:50:46.851: E/AndroidRuntime(2125): at sourcecode.jazzplayer.FragmentSongs.onAttach(FragmentSongs.java:128) 09-19 23:50:46.851: E/AndroidRuntime(2125): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:883) 09-19 23:50:46.851: E/AndroidRuntime(2125): at android.support.v4.app.FragmentManagerImpl.performPendingDeferredStart(FragmentManager.java:823) 09-19 23:50:46.851: E/AndroidRuntime(2125): at android.support.v4.app.Fragment.setUserVisibleHint(Fragment.java:819) 09-19 23:50:46.851: E/AndroidRuntime(2125): at android.support.v4.app.FragmentPagerAdapter.setPrimaryItem(FragmentPagerAdapter.java:130) 09-19 23:50:46.851: E/AndroidRuntime(2125): at android.support.v4.view.ViewPager.populate(ViewPager.java:1066) 09-19 23:50:46.851: E/AndroidRuntime(2125): at android.support.v4.view.ViewPager.populate(ViewPager.java:914) 09-19 23:50:46.851: E/AndroidRuntime(2125): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436) 09-19 23:50:46.851: E/AndroidRuntime(2125): at android.view.View.measure(View.java:12603) 09-19 23:50:46.851: E/AndroidRuntime(2125): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4677) 09-19 23:50:46.851: E/AndroidRuntime(2125): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293) 09-19 23:50:46.851: E/AndroidRuntime(2125): at android.view.View.measure(View.java:12603) 09-19 23:50:46.851: E/AndroidRuntime(2125): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4677) 09-19 23:50:46.851: E/AndroidRuntime(2125): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1369) 09-19 23:50:46.851: E/AndroidRuntime(2125): at android.widget.LinearLayout.measureVertical(LinearLayout.java:660) 09-19 23:50:46.851: E/AndroidRuntime(2125): at android.widget.LinearLayout.onMeasure(LinearLayout.java:553) 09-19 23:50:46.851: E/AndroidRuntime(2125): at android.view.View.measure(View.java:12603) 09-19 23:50:46.851: E/AndroidRuntime(2125): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4677) 09-19 23:50:46.851: E/AndroidRuntime(2125): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293) 09-19 23:50:46.851: E/AndroidRuntime(2125): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2072) 09-19 23:50:46.851: E/AndroidRuntime(2125): at android.view.View.measure(View.java:12603) 09-19 23:50:46.851: E/AndroidRuntime(2125): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1044) 09-19 23:50:46.851: E/AndroidRuntime(2125): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2418) 09-19 23:50:46.851: E/AndroidRuntime(2125): at android.os.Handler.dispatchMessage(Handler.java:99) 09-19 23:50:46.851: E/AndroidRuntime(2125): at android.os.Looper.loop(Looper.java:137) 09-19 23:50:46.851: E/AndroidRuntime(2125): at android.app.ActivityThread.main(ActivityThread.java:4340) 09-19 23:50:46.851: E/AndroidRuntime(2125): at java.lang.reflect.Method.invokeNative(Native Method) 09-19 23:50:46.851: E/AndroidRuntime(2125): at java.lang.reflect.Method.invoke(Method.java:511) 09-19 23:50:46.851: E/AndroidRuntime(2125): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 09-19 23:50:46.851: E/AndroidRuntime(2125): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 09-19 23:50:46.851: E/AndroidRuntime(2125): at dalvik.system.NativeStart.main(Native Method) </code></pre> <p>the error is in this line of code</p> <p>TextView text=(TextView)view.findViewById(R.id.tt); //this line is in the receiver fragment (fragmentArtist)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload