Reputation: 51
i have a lite problem with my code, i was search in this site but still not found the best answer i will send data to my tab fragment, this my code
My Activity class
public class MainActivity extends AppCompatActivity
private ViewPager pager;
private Toolbar toolbar;
private TabLayout tabs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pager=(ViewPager) findViewById(R.id.pager);
tabs=(TabLayout) findViewById(R.id.tabs);
pager.setAdapter(new TabFragmentPagerAdapter(getSupportFragmentManager()));
tabs.setupWithViewPager(pager);
tabs.setTabGravity(TabLayout.GRAVITY_FILL);
Bundle bundle = new Bundle();
bundle.putString("edttext", "data From Activity");
// set Fragmentclass Arguments
tab1Fragment fragobj = new tab1Fragment();
fragobj.setArguments(bundle);
}
}
and this my tab1 class
public class tab1Fragment extends Fragment
public tab1Fragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view= inflater.inflate(R.layout.fragment_tab1, container, false);
TextView tes=(TextView) view.findViewById(R.id.testtab1);
String strtext = getArguments().getString("edttext");
tes.setText("tes :"+ strtext);
return view;
}
}
and this my page fragment adapter
public class TabFragmentPagerAdapter extends FragmentPagerAdapter
String[] title = new String[]{
"Tab 1", "Tab 2", "Tab 3"
};
public TabFragmentPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position){
Fragment fragment=null;
switch (position){
case 0:
fragment=new tab1Fragment();
break;
case 1:
fragment=new tab2Fragment();
break;
case 2:
fragment=new tab3Fragment();
break;
default:
fragment=null;
break;
}
return fragment;
}
@Override
public CharSequence getPageTitle(int position){
return title[position];
}
@Override
public int getCount(){
return title.length;
}
}
when i run error
java.lang.NullPointerException
Please Help
Upvotes: 3
Views: 15622
Reputation: 1
I also suffered with this problem. It earned it for me. I declared in the fragment activity
public static String customerId;
and in the fragment I received referring simply by writing
customerId
I'm not good at programming. Maybe this will help someone
Upvotes: 0
Reputation: 307
Maybe its late answer, but there is something that worked for me:
send your bundle to adapter:
Bundle bundle = new Bundle();
bundle.putString("edttext", "data From Activity");
pager.setAdapter(new TabFragmentPagerAdapter(getSupportFragmentManager(), bundle));
extend a constructor in your adapter:
Bundle bundle;
public TabFragmentPagerAdapter(FragmentManager fm, Bundle bundle) {
super(fm);
this.bundle=bundle;
}
and send it to the fragment:
switch (position){
case 0:
fragment=new tab1Fragment();
fragment.setArguments(bundle);
break;
hopefully, it helps. }
Upvotes: -1
Reputation: 749
Create a function in the fragment to pass in the data from activity just like you do in getters
and setters
for object classes like so:
// Fragment
class TabFragment extends Fragment{
public String myDataString;
...
public void setTabInfo(String myActivityString){
this.myDataString = myActivityString;
}
}
// Activity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
TabFragment frag = new TabFragment();
frag.setTabInfo("data From Activity");
}
I prefer this method since you pass the data directly without having to serialize the object in the case of using Bundle
in the case of objects
Upvotes: 0
Reputation: 5534
May be you are getting NullPointerException
because your fragment getting initialised again inside getItem
without argument.
Try this way.
@Override
public Fragment getItem(int position){
Fragment fragment=null;
Bundle bundle = new Bundle();
bundle.putString("edttext", "data From Activity");
switch (position){
case 0:
fragment=new tab1Fragment();
fragment.setArguments(bundle);
break;
case 1:
fragment=new tab2Fragment();
break;
case 2:
fragment=new tab3Fragment();
break;
default:
fragment=null;
break;
}
return fragment;
}
Note : make sure you are importing same versions of Fragment in both MainActivity and tab1Fragment.
Upvotes: 1
Reputation: 2003
In your AppCompatActivity
you have send
tab1Fragment fragobj = new tab1Fragment();
fragobj.setArguments(bundle);
but in your TabFragmentPagerAdapter
fragment=new tab1Fragment();
break;
it has null arguments
inside switch
switch (position){
case 0:
fragment=new tab1Fragment();
Bundle bundle = new Bundle();
bundle.putString("edttext", "data From Activity");
fragment.setArguments(bundle);
break;
Upvotes: 6
Reputation: 4182
I think You Start the Tab1Frament() with On call of Youractivity first You can do it...
Used this..
if (savedInstanceState == null) {
FragmentTransaction ft = this.getSupportFragmentManager().beginTransaction();
tab1Fragment fragobj = new tab1Fragment();
Bundle bundle = new Bundle();
bundle.putString("edttext", "data From Activity");
// set Fragmentclass Arguments
fragobj.setArguments(bundle);
ft.replace(R.id.frame, fragobj);
ft.addToBackStack(null);
ft.commit();
}
Instead of
Bundle bundle = new Bundle();
bundle.putString("edttext", "data From Activity");
// set Fragmentclass Arguments
tab1Fragment fragobj = new tab1Fragment();
fragobj.setArguments(bundle);
Upvotes: 0