bundle.getString返回一个空字符串

时间:2019-05-12 11:01:05

标签: android arguments bundle

我无法理解将参数从活动传递到片段的错误之处。

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();

在我的片段中,我尝试在onCreateView方法中采用这种方式,但字符串始终为空

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;

在评论中被询问后添加的代码,我不会以任何方式操纵字符串

2 个答案:

答案 0 :(得分:0)

根据您的活动

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();

这是片段中的内容。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    String text = getArguments().getString("name");    
    return inflater.inflate(R.layout.fragment, container, false);
}

答案 1 :(得分:0)

代替

Bundle bundle = getArguments();

使用此

Bundle bundle = this.getArguments();