There is an adapter

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

In the Activation Class I install two fragments to the ViewPager

 viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager()); viewPagerAdapter.addFragments(new ListWordFragment(), "Список"); viewPagerAdapter.addFragments(new AddWordFragment(), "Добавить слово"); viewPager.setAdapter(viewPagerAdapter); tabLayout.setupWithViewPager(viewPager); 

And in the Class of the same activit, I get the second fragment and in the TextView I set the text

 Fragment fragment = viewPagerAdapter.getItem(1); ((TextView) fragment.getView().findViewById(R.id.info2_textView)).setText("Loll"); 

And when executing the code, I get an error that the text returns null

 06-27 16:20:02.916 22266-22266/ru.landmarkstd.educt E/AndroidRuntime: FATAL EXCEPTION: main Process: ru.landmarkstd.educt, PID: 22266 Theme: themes:{default=overlay:com.baranovgroup.nstyle, iconPack:com.baranovgroup.nstyle, fontPkg:com.baranovgroup.nstyle, com.android.systemui=overlay:com.baranovgroup.nstyle, com.android.systemui.navbar=overlay:com.baranovgroup.nstyle} java.lang.RuntimeException: Unable to start activity ComponentInfo{ru.landmarkstd.educt/ru.landmarkstd.educt.Word}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2450) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2510) at android.app.ActivityThread.-wrap11(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5461) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference at ru.landmarkstd.educt.Word.onCreate(Word.java:60) at android.app.Activity.performCreate(Activity.java:6251) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2403) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2510) at android.app.ActivityThread.-wrap11(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5461) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

The first fragment Class

 public class ListWordFragment extends Fragment { public View v; public ListView listView; ListViewClearAdapter listAdapter; ArrayList<itemSelect> itemSelects = new ArrayList<itemSelect>(); public ListWordFragment() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { v = inflater.inflate(R.layout.fragment_list_word, container, false); listView = (ListView) v.findViewById(R.id.listView); itemSelects.add(new itemSelect("test1", "Org","", false)); itemSelects.add(new itemSelect("test2", "Org","", false)); itemSelects.add(new itemSelect("test3", "Org","", false)); listAdapter = new ListViewClearAdapter(v.getContext(), itemSelects); listView.setAdapter(listAdapter); return v; } } 

XML

 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="ru.landmarkstd.educt.Fragments.ListWordFragment"> <!-- TODO: Update blank fragment layout --> <ListView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/listView" /> </FrameLayout> 

Second fragment class

  public class AddWordFragment extends Fragment { public AddWordFragment() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_add_word, container, false); } } 

XML

 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="ru.landmarkstd.educt.Fragments.AddWordFragment"> <!-- TODO: Update blank fragment layout --> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/Word_editText" android:layout_gravity="center_horizontal" android:hint="Слово" android:gravity="center" android:theme="@style/MyEditText"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="*Внесите в скобки (Квадратные или круглые) букву которую надо заменить, пример: С(о)рока" android:id="@+id/Info1_textView" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:layout_marginBottom="5dp" android:layout_marginTop="2dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Варианты ответов" android:id="@+id/text1_textView" android:layout_gravity="center_horizontal" android:layout_marginTop="10dp" android:textSize="18dp" /> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="5dp" > <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/Answer_editText" android:layout_weight="1" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:hint="Ответ" android:gravity="center" android:theme="@style/MyEditText"/> <Button android:layout_width="146dp" android:layout_height="wrap_content" android:text="Добавить ответ" android:id="@+id/addAnswer_button" android:background="#6acbda" android:textColor="#ffffff" android:layout_marginRight="5dp" /> </LinearLayout> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="*Отметьте правильный ответ" android:id="@+id/info2_textView" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" /> <ListView android:layout_width="match_parent" android:layout_height="378dp" android:id="@+id/listView" android:layout_marginTop="5dp" android:layout_gravity="center_vertical" android:layout_weight="1" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Добавить слово" android:id="@+id/addWord_button" android:background="#5dc8d8" android:textColor="#ffffff" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:layout_marginBottom="5dp" android:layout_marginTop="5dp" /> </LinearLayout> </FrameLayout> 

Word class

 public class Word extends AppCompatActivity { Toolbar toolbar; TabLayout tabLayout; ViewPager viewPager; ViewPagerAdapter viewPagerAdapter; String type; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_word); toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); new NavigationDrawer(this, toolbar, 0); tabLayout = (TabLayout) findViewById(R.id.tabLayout); viewPager = (ViewPager) findViewById(R.id.viewPager); type = getIntent().getStringExtra("type"); switch (type) { case "add": getSupportActionBar().setTitle("Добавление списка"); break; case "edit": getSupportActionBar().setTitle("Редактирование списка"); break; } viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager()); viewPagerAdapter.addFragments(new ListWordFragment(), "Список"); viewPagerAdapter.addFragments(new AddWordFragment(), "Добавить слово"); viewPager.setAdapter(viewPagerAdapter); tabLayout.setupWithViewPager(viewPager); Fragment fragment = viewPagerAdapter.getItem(1); Log.d("myLog", String.valueOf(fragment.getView())); } } 
  • Please discard the fragment code as one and the other, with their xml layout - Evgeny Suetin
  • @ EvgenySuetin Updated - Andrei
  • What is the Word class? - temq
  • @temq This is the main activit more precisely the class of the main activit - Andrei
  • Show his code - temq

0