I have some string variable that I want to pass to the tab fragment
I tried through the bundle - swears at a NullPointerException, however, I received this variable from this activity in this activity, so there my favorite is not null.
Here is my activity's OnCreate code.

protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_extended); // Creating The Toolbar and setting it as the Toolbar for the activity tab1 = new Tab1(); toolbar = (Toolbar) findViewById(R.id.tool_bar); setSupportActionBar(toolbar); getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setDisplayShowHomeEnabled(true); // Creating The ViewPagerAdapter and Passing Fragment Manager, Titles fot the Tabs and Number Of Tabs. adapter = new ViewPagerAdapter(getSupportFragmentManager(),Titles,Numboftabs); // Assigning ViewPager View and setting the adapter pager = (ViewPager) findViewById(R.id.pager); pager.setAdapter(adapter); // Assiging the Sliding Tab Layout View tabs = (SlidingTabLayout) findViewById(R.id.tabs); tabs.setDistributeEvenly(true); // To make the Tabs Fixed set this true, This makes the tabs Space Evenly in Available width // Setting Custom Color for the Scroll bar indicator of the Tab View tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() { @Override public int getIndicatorColor(int position) { return getResources().getColor(R.color.tabsScrollColor); } }); // Setting the ViewPager For the SlidingTabsLayout tabs.setViewPager(pager); Bundle extras = getIntent().getExtras(); if (extras != null) { String href = extras.getString("href"); link=href; String name = extras.getString("name"); label=name; } ExtendedActivity.this.setTitle(label); Bundle bundle = new Bundle(); bundle.putString("link", link); tab1.setArguments(bundle); 

Actually here it is written through bundle
And here is the code for the fragment Tab1:

 public class Tab1 extends Fragment { String Link=""; // TODO: Rename parameter arguments, choose names that match // the fragment initialization parameters, eg ARG_ITEM_NUMBER private static final String ARG_PARAM1 = "param1"; private static final String ARG_PARAM2 = "param2"; // TODO: Rename and change types of parameters private String mParam1; private String mParam2; private OnFragmentInteractionListener mListener; public Tab1() { // Required empty public constructor } /** * Use this factory method to create a new instance of * this fragment using the provided parameters. * * @param param1 Parameter 1. * @param param2 Parameter 2. * @return A new instance of fragment FragmentSettings. */ // TODO: Rename and change types and number of parameters public static Tab1 newInstance(String param1, String param2) { Tab1 fragment = new Tab1(); Bundle args = new Bundle(); args.putString(ARG_PARAM1, param1); args.putString(ARG_PARAM2, param2); fragment.setArguments(args); return fragment; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getArguments() != null) { mParam1 = getArguments().getString(ARG_PARAM1); mParam2 = getArguments().getString(ARG_PARAM2); } Link = getArguments().getString("link"); this.getActivity().setTitle(Link); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.tab1, container, false); } // TODO: Rename method, update argument and hook method into UI event public void onButtonPressed(Uri uri) { if (mListener != null) { mListener.onFragmentInteraction(uri); } } public interface OnFragmentInteractionListener { // TODO: Update argument type and name void onFragmentInteraction(Uri uri); } } 

Help me figure out what to do here?


Edit1:

  protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_extended); Bundle extras = getIntent().getExtras(); if (extras != null) { link = extras.getString("href"); label = extras.getString("name"); } tab1 = Tab1.newInstance(link, null); // Creating The Toolbar and setting it as the Toolbar for the activity // tab1 = new Tab1(); toolbar = (Toolbar) findViewById(R.id.tool_bar); setSupportActionBar(toolbar); getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setDisplayShowHomeEnabled(true); // Creating The ViewPagerAdapter and Passing Fragment Manager, Titles fot the Tabs and Number Of Tabs. adapter = new ViewPagerAdapter(getSupportFragmentManager(),Titles,Numboftabs); // Assigning ViewPager View and setting the adapter pager = (ViewPager) findViewById(R.id.pager); pager.setAdapter(adapter); // Assiging the Sliding Tab Layout View tabs = (SlidingTabLayout) findViewById(R.id.tabs); tabs.setDistributeEvenly(true); // To make the Tabs Fixed set this true, This makes the tabs Space Evenly in Available width // Setting Custom Color for the Scroll bar indicator of the Tab View tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() { @Override public int getIndicatorColor(int position) { return getResources().getColor(R.color.tabsScrollColor); } }); // Setting the ViewPager For the SlidingTabsLayout tabs.setViewPager(pager); ExtendedActivity.this.setTitle(label); } 

and this is Tab1:

 public class Tab1 extends Fragment { // TODO: Rename parameter arguments, choose names that match // the fragment initialization parameters, eg ARG_ITEM_NUMBER private static final String ARG_PARAM1 = "link"; private static final String ARG_PARAM2 = "param2"; // TODO: Rename and change types of parameters private String mParam1; private String mParam2; private OnFragmentInteractionListener mListener; public Tab1() { // Required empty public constructor } /** * Use this factory method to create a new instance of * this fragment using the provided parameters. * * @param param1 Parameter 1. * @param param2 Parameter 2. * @return A new instance of fragment FragmentSettings. */ // TODO: Rename and change types and number of parameters public static Tab1 newInstance(String param1, String param2) { Tab1 fragment = new Tab1(); Bundle args = new Bundle(); args.putString(ARG_PARAM1, param1); args.putString(ARG_PARAM2, param2); fragment.setArguments(args); return fragment; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getArguments() != null) { mParam1 = getArguments().getString(ARG_PARAM1); mParam2 = getArguments().getString(ARG_PARAM2); } this.getActivity().setTitle(mParam1); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.tab1, container, false); } // TODO: Rename method, update argument and hook method into UI event public void onButtonPressed(Uri uri) { if (mListener != null) { mListener.onFragmentInteraction(uri); } } public interface OnFragmentInteractionListener { // TODO: Update argument type and name void onFragmentInteraction(Uri uri); } 

    3 answers 3

    Try this:

    1) pull out extras after the line setContentView(R.layout.activity_extended);

    2) remove the variables String href and String name from the condition body

    3) create an instance of class Tab1 through the factory method Tab1.newInstance()

     protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_extended); String href; String name; Intent intent = getIntent(); if (intent != null) { link = href = intent.getString("href"); label = name = intent.getString("name"); } tab1 = Tab1.newInstance(link, null); ... } 

    Still, I would not advise giving names to variables beginning with a capital letter ( String Link=""; )

    well, and small edits in the class Tab1

     private static final String ARG_PARAM1 = "link"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getArguments() != null) { mParam1 = getArguments().getString(ARG_PARAM1); mParam2 = getArguments().getString(ARG_PARAM2); } } @Override public void onAttach (Context context) { super.onAttach(context); this.getActivity().setTitle(mParam1); } 
    • Hurray, thanks to your comment, the nullPointerException error has stopped getting out, but the Tab1 header is now empty. Maybe I overlooked something, update the topic with the accepted changes - Alex
    • one
      And in class Tab1 1) changed the value of the constant ARG_PARAM1 ? 2) passed to the setTitle method mParam1 ? - Ivan Dembicki
    • updated the topic, like everything has changed - Alex
    • one
      Well, it doesn’t hurt to write down the string value at each step in the log in order to narrow the search range - Ivan Dembicki
    • one
      this.getActivity().setTitle(mParam1); comes before return inflater.inflate(R.layout.tab1, container, false); ? Show the code. - Ivan Dembicki

    you have a direct link to your piece

     tab1 = new Tab1(); 

    Create a method in the fragment

     public void setData(String data) { //делайте все, что нужно с этими данными } 

    And the right place in the Activity call

     tab1.setData("строка, которую нужно передать"); 
    • Damn, anyway, NullPointerException, in the lines where I pass the line and do actions with the line in the fragment - Alex
    • @Alex in which line of Exception ? - Vladyslav Matviienko

    Do

     tab1 = Tab1.newInstance(data); 

    NullPointerException crashes, because in onCreateView you are trying to get arguments, and if you create a fragment using new Tab1 , they will be null .