There is a MainActivity and two fragments that are in PlayerActivity (i.e. PlayerActivity consists of two fragments). In the main (MainActivity) in RecyclerView list is displayed from the database. How can I send an id to an entry in both fragments when I click on a list item? Now, in the onItemClick id method, the records are passed to PlayerActivity, which does not contain fragments, but I redid the activity with the player and now I need to pass id into both fragments in order to display recording data such as the song name, the player, etc. in one fragment. , and in the second activity, the lyrics
Right now everything is so arranged for me .. Specially created a new project to understand how to transfer data to fragments
Fragment1
public class Fragment1 extends Fragment { public static final String ARG1 = "arg1"; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment1, container,false); Bundle args = getArguments(); TextView txtOne = (TextView) rootView.findViewById(R.id.txtOne); txtOne.setText(args.getString(ARG1)); return rootView; } } Fragragment2
public class Fragment2 extends Fragment { public static final String ARG2 = "arg2"; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment2, container,false); Bundle args = getArguments(); TextView txtTwo = (TextView) rootView.findViewById(R.id.txtTwo); txtTwo.setText(args.getString(ARG2)); return rootView; } } MyPagerAdapter
public class MyPagerAdapter extends FragmentPagerAdapter { public MyPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int i) { Fragment fragment1 = new Fragment1(); Fragment fragment2 = new Fragment2(); Bundle args = new Bundle(); if (i == 0) { args.putString(Fragment1.ARG1, "Это первый фрагмент"); fragment1.setArguments(args); return fragment1; } else { args.putString(Fragment2.ARG2, "Это второй фрагмент"); fragment2.setArguments(args); return fragment2; } } @Override public int getCount() { return 2; } } A method from MainActivity that sends id entries to PlayerActivity.
@Override public void onItemClick(int pos) { Intent intent = new Intent(this, PlayerActivity.class); intent.putExtra("id", pos); startActivity(intent); } If I try to pass an id from PlayerActivity or MainActivity in this way and remove the arguments to fragments in the class MyPagerAdapter
Bundle args = new Bundle(); args.putLong(Fragment1.ARG1, id); Fragment1 f1 = new Fragment1(); f1.setArguments(args); it gives an error
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString (java.lang.String)' on a null object reference at ru.test.viewpager.Fragment1.onCreateView (Fragment1. java: 23) at android.support.v4.app.Fragment.performCreateView (Fragment.java:2354) at android.support.v4.app.FragmentManagerImpl.moveToState (FragmentManager.java:1419) at android.support.v4.app .FragmentManagerImpl.moveFragmentToExpectedState (FragmentManager.java:1740) at android.support.v4.app.FragmentManagerImpl.moveToState (FragmentManager.java:1809) at android.support.v4.app.ackStachart, ec4884app, 17194004.app.FragmentManagerImpl. android.support.v4.app.FragmentManagerImpl.executeOps (FragmentManager.java:2580) at android.support.v4.app.FragmentManagerImpl.executeOpsTogether (FragmentManager.java:2367) at android.support.v4.app.http.a. FragmentManager.java:2322) at android.support.v4.app.FragmentManagerImpl.execSingleAction (FragmentM anager.java:2199) at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss (BackStackRecord.java:651) at android.support.v4.app.FragmentPagerAdapter.finishUpdate (FragmentPagerAdapter.vava.app.FragmentPagerAdapter.finishUpdate (FragmentPagerAdapter.vava.defragmentPaAdd.FementPagerAdapter.finishUpdate (FragmentPagerAdapter.v4.app.FragmentPagerAdapter.finishUpdate) .view.ViewPager.populate (ViewPager.java:1236) at android.support.v4.view.ViewPager.populate (ViewPager.java:1084) at android.support.v4.view.ViewPager.onMeasure (ViewPager.java:1614 ) at android.view.View.measure (View.java:21125) at android.widget.RelativeLayout.measureChildHorizontal (RelativeLayout.java:715) at android.widget.RelativeLayout.onMeasure (RelativeLayout.java penet61) at android.view .View.measure (View.java:21125) at android.view.ViewGroup.measureChildWithMargins (ViewGroup.java:6461) at android.widget.FrameLayout.onMeasure (FrameLayout.java:185) at android.support.v7.widget. ContentFrameLayout.onMeasure (ContentFrameLayout.java:139) at android.view.View.measure (View.java:21125) at android.view.ViewGroup.measureChildWithMargins (ViewGroup.java:6461) at android.suppo rt.v7.widget.ActionBarOverlayLayout.onMeasure (ActionBarOverlayLayout.java:400) at android.view.View.measure (View.java:21125) at android.view.ViewGroup.measureChildWithMargins (ViewGroup.java:6461) at android.with .FrameLayout.onMeasure (FrameLayout.java:185) at android.view.View.measure (View.java:21125) at android.view.ViewGroup.measureChildWithMargins (ViewGroup.java:6461) at android.widget.LinearLayout.measureChildBeforeLeoutLeout LinearLayout.java:1464) at android.widget.LinearLayout.measureVertical (LinearLayout.java:758) at android.widget.LinearLayout.onMeasure (LinearLayout.java:640) at android.view.View.measure (View.java:21125 ) at android.view.ViewGroup.measureChildWithMargins (ViewGroup.java:6461) at android.widget.FrameLayout.onMeasure (FrameLayout.java:185) at com.android.internal.policy.DecorView.onMeasure (DecorView.java:896) at android.view.View.measure (View.java:21125) at android.view.ViewRootImpl.performMeasure (ViewRootImpl.java:2612) at android.view.ViewRootImpl.measureHierarchy (V iewRootImpl.java:1664) at android.view.ViewRootImpl.performTraversals (ViewRootImpl.java:1915) at android.view.ViewRootImpl.doTraversal (ViewRootImpl.java:1537) at android.view.ViewRootImpl $ Traeraftrampl.java:1537 at android.view.ViewRootImpl $ Traeraphrampl.java:1537 at android.view.ViewRootImpl $ TraerampImpl.java:1537 at android.view.ViewRootImpl $ TraerampImpl.java:1537 at android.view.ViewRootImpl $ TraeImpl.java:1537 at android.view.viewRootImpl $ TraeImpl.doTraversal : 7183) at android.view.Choreographer $ CallbackRecord.run (Choreographer.java:959) at android.view.Choreographer.doCallbacks (Choreographer.java:734) at android.view.Choreographer.doFrame (Choreographer.j4, 34) at android.view.Choreographer.doFrame (Choreographer.j4, 34) at android.view.Choreographer.doFrame (Choreographer.j4 at android.view.Choreographer $ FrameDisplayEventReceiver.run (Choreographer.java:945) at android.os.Handler.handleCallback (Handler.java:751) at android.os.Handler.dispatchMessage (Handler.java:95) at android. os.Looper.loop (Looper.java:15) at android.app.ActivityThread.main (ActivityThread.java:6776) at java.lang.reflect.Method.invoke (Native Method) at com.android.internal.os. ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:1518) 01-15 22: 44: 12.598 7458-7458 / ru.test.viewpager E / AndroidRuntime:
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1408)
On the line txtOne.setText(args.getString(ARG1));