Yasin Enu Yasin Enu - 1 year ago 141
Android Question

How to send string from activity to fragment using bundle

when i try to run this code, it show me an exception and it is "nullPointer exception". I can't find the bug of this code, can anyone help me to fix that bug please? my code is blow please help me to fix it.

here is my MainActivity

public class MainActivity extends AppCompatActivity {

private TabLayout tabLayout;
private ViewPager viewPager;
EditText testInputTV;
Button send;
Bundle bundle;
String welcome ="Welcome";

protected void onCreate(Bundle savedInstanceState) {

testInputTV = (EditText) findViewById(R.id.testInput);

send= (Button) findViewById(R.id.send);

viewPager = (ViewPager) findViewById(R.id.viewpager);

tabLayout = (TabLayout) findViewById(R.id.tabs);


public void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());

bundle =new Bundle();
bundle.putString("welcome", welcome);
FragmentOne fragobj = new FragmentOne();
adapter.addFragment(new FragmentOne(), "ONE");


class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();

public ViewPagerAdapter(android.support.v4.app.FragmentManager manager) {

public Fragment getItem(int position) {
return mFragmentList.get(position);

public int getCount() {
return mFragmentList.size();

public void addFragment(Fragment fragment, String title) {

public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);


my Fragment class

public class FragmentOne extends Fragment {

String storeText;
private Bundle bundle;

public FragmentOne() {

public void onCreate(Bundle savedInstanceState) {


public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view=inflater.inflate(R.layout.fragment_fragment_one, container, false);
bundle = this.getArguments();
storeText = bundle.getString("welcome");

return view;


Error text in Logcat

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference

Answer Source

You're adding a new fragment instead of using the one you made and set the arguments to:

adapter.addFragment(new FragmentOne(), "ONE");

Change this to:

adapter.addFragment(fragobj, "ONE");

Also it is recommended to use Abstract Factory design pattern with newInstance method when instantiating a fragment, something like:

public static FragmentOne newInstance(Bundle b) {
    FragmentOne fragment = new FragmentOne();

    return fragment;

And then create your fragment like:

FragmentOne fragobj = FragmentOne.newInstance(bundle);

And in this line you're setting arguments and creating it as well.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download