简体   繁体   English

bundle.getString返回一个空字符串

[英]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.

相关问题 Bundle.getString()返回空值? - Bundle.getString() returning null? bundle.getString()返回空值 - bundle.getString() return null value Bundle.getString(字符串键)的默认值 - Default value on Bundle.getString(String key) NoSuchMethodError Bundle.getString() - NoSuchMethodError Bundle.getString() 为什么 bundle.getString() 为另一个片段接收的数据获取空值? - Why is bundle.getString() getting null for data that received by another fragment? 将bundle.getString()的结果分配给String会引发ClassCastException - Assigning the result of bundle.getString() to a String throws a ClassCastException android意图数据使用if(!bundle.getString(“ ACCOUNT”)。equals(null))但出错 - android intent data use if(!bundle.getString(“ACCOUNT”).equals(null) ) but error bundle.getString() 和 intent.getStringExtra() 有什么区别? - What is difference between bundle.getString() and intent.getStringExtra()? 在Bundle.getString()中设置密钥时发生错误 - Get an error when setting the key in Bundle.getString() 我该如何解决“方法调用&#39;bundle.getString(“ Ans_age”)。equals(R.string.age2)&#39;可能会产生&#39;java.lang.NullPointerException&#39;” - How can I solve this “Method invocation 'bundle.getString(”Ans_age“).equals(R.string.age2)' may produce 'java.lang.NullPointerException'”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM