[英]bundle.getString returns a null string
I can't understand where I'm wrong in passing arguments from an activity to a fragment. 我无法理解将参数从活动传递到片段的错误。
In the MyActivity.class
在
MyActivity.class
Bundle bundle = new Bundle();
bundle.putString("name", "helloworld");
HomeFragment homefragment = new HomeFragment();
homefragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, homefragment).commit();
And in my fragment I try to take the arguments this way inside the onCreateView method but the string is always null 在我的片段中,我尝试在onCreateView方法中以这种方式获取参数,但字符串始终为null
onCreateView()
Bundle bundle = this.getArguments();
if(bundle != null)
mName = bundle.getString("name"); //mName here is null, checked with a simple print
//this is for the viewpager
models = new ArrayList<>();
Resources res = getResources();
mDescriptions = res.getStringArray(R.array.newGameDescription);
mTitle = res.getStringArray(R.array.newGame);
models.add(new Model(R.drawable.brochure,mTitle[0], mDescriptions[0]));
models.add(new Model(R.drawable.sticker,mTitle[1], mDescriptions[1]));
models.add(new Model(R.drawable.poster,mTitle[2], mDescriptions[2]));
models.add(new Model(R.drawable.namecard,mTitle[3], mDescriptions[3]));
adapter = new ViewPagerAdapter(getContext(), models, mName); // I pass the string value here to pass it to an activity (with putextra) after clicking on a page of the viewer
View view = inflater.inflate(R.layout.fragment_home,container,false);
viewPager = (ViewPager) view.findViewById(R.id.gameModeViewPager);
viewPager.setAdapter(adapter);
viewPager.setPadding(130,0,130,0);
Integer[] colors_temp = {
getResources().getColor(R.color.prova1),
getResources().getColor(R.color.prova2),
getResources().getColor(R.color.prova3),
getResources().getColor(R.color.prova4),
};
colors = colors_temp;
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int i, float v, int i1) {
if (i < (adapter.getCount() - 1) && i < (colors.length -1)){
viewPager.setBackgroundColor((Integer)argbEvaluator.evaluate(v, colors[i], colors[i+1]));
}else{
viewPager.setBackgroundColor(colors[colors.length -1]);
}
}
@Override
public void onPageSelected(int i) {
}
@Override
public void onPageScrollStateChanged(int i) {
}
});
return view;
Code added after being asked in the comments, I do not manipulate the string in any way 在评论中询问后添加了代码,我没有以任何方式操纵字符串
From your activity 从你的活动
Bundle b = new Bundle();
b.putString("name", "123456789");
FragmentA fragmentA = new FragmentA ();
fragmentA .setArguments(b);
fm.beginTransaction()
.replace(R.id.fragment_container, new FragmentA())
.commit();
And this in the fragment. 而这在片段中。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String text = getArguments().getString("name");
return inflater.inflate(R.layout.fragment, container, false);
}
Instead of 代替
Bundle bundle = getArguments();
Use this 用这个
Bundle bundle = this.getArguments();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.