Yasin Enu Yasin Enu - 3 months ago 37
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";



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


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

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

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

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


}

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

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

}



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) {
super(manager);
}

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

@Override
public int getCount() {
return mFragmentList.size();
}

public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}

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


}


my Fragment class

public class FragmentOne extends Fragment {

String storeText;
private Bundle bundle;

public FragmentOne() {
}


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

}

@Override
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");
Toast.makeText(getContext().getApplicationContext(),storeText,Toast.LENGTH_SHORT).show();

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

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();
    fragment.setArguments(b);

    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.

Comments