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));

    1 answer 1

    Try the following:

    PlayerActivity:

     public class PlayerActivity extends AppCompatActivity { private MyPagerAdapter adapter; private ViewPager viewPager; private TabLayout tabLayout; private String id = ""; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_player); viewPager = findViewById(R.id.viewPager); tabLayout = findViewById(R.id.tabLayout); id = getIntent().getStringExtra("id"); setupViewPager(); tabLayout.setupWithViewPager(viewPager); } private void setupViewPager() { adapter = new MyPagerAdapter(getSupportFragmentManager()); adapter.addFragment(Fragment1.newInstance(id),"Первый фрагмент"); adapter.addFragment(Fragment2.newInstance(id),"Второй фрагмент"); viewPager.setAdapter(adapter); }} 

    activity_player.xml:

     <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v4.view.ViewPager android:id="@+id/viewPager" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="8dp"> <android.support.design.widget.TabLayout android:id="@+id/tabLayout" style="@style/TabLayoutStyle" android:layout_width="match_parent" android:layout_height="32dp"/> </android.support.v4.view.ViewPager> 

    MyPagerAdapter:

     public class MyPagerAdapter extends FragmentPagerAdapter { private List<Fragment> fragments = new ArrayList<>(); private List<String> titles = new ArrayList<>(); public MyPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { return fragments.get(position); } @Override public int getCount() { return fragments.size(); } @Nullable @Override public CharSequence getPageTitle(int position) { return titles.get(position); } void addFragment(Fragment fragment, String title) { fragments.add(fragment); titles.add(title); }} 

    Your fragment:

     public class Fragment1 extends Fragment { public static Fragment1 newInstance(String id) { Bundle args = new Bundle(); args.putString("ID", id); Fragment1 fragment1 = new Fragment1(); fragment1.setArguments(args); return fragment1; } @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment1, container, false); } @Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); String id = getArguments().getString("ID"); //И далее доставайте из базы нужную информацию по ID }} 

    The second fragment by analogy with the first.

    • Why TabLayout? I do not want tabs - Kolhoznik
    • Then it’s just without him, and the title is popping out everywhere, I’m already writing on the machine. - alexey antonov
    • I looked through your answer .. now I will try to do without a Title according to your answer - Kolhoznik
    • An example is a worker, he did it on Kotlin a week ago, he could have made a mistake in translating into Java, well, yes, the environment will tell, I think, where necessary. And I had the viewer in the fragment, not in the activation, but there the difference is only in the transfer to the childFragmentManager adapter, here you have the usual supportFragmentManager - alexey antonov
    • one
      we pass the parameters to the static fragment method newInstance (String id), for each fragment it has its own, pass as many different parameters as you like into each (within reasonable limits). And I understood what you were talking about, it would be possible not to make a String id local variable in PlayerActivity's onCreate () method and put it out as a class field and pull it directly from the setupViewPager () method without passing it as an argument, you can also declare a bunch other fields. - alexey antonov