如何使用bundle将字符串从活动发送到片段

时间:2016-08-15 18:44:06

标签: android android-fragments nullpointerexception bundle

当我尝试运行此代码时,它会向我显示一个异常,它是" nullPointer exception"。我无法找到此代码的错误,有人可以帮我修复该错误吗?我的代码很好,请帮我解决。

这是我的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);
        }
    }


}

我的片段类

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;
    }


}

Logcat中的错误文本

 java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference

4 个答案:

答案 0 :(得分:1)

问题出在这里

adapter.addFragment(new FragmentOne(), "ONE");

因为您正在创建一个新对象而不是旧对象。

尝试以下代码:

adapter.addFragment(fragobj, "ONE");

答案 1 :(得分:1)

您正在添加新片段而不是使用您创建的片段,并将参数设置为:

adapter.addFragment(new FragmentOne(), "ONE");

将其更改为:

adapter.addFragment(fragobj, "ONE");

在实例化片段时,建议使用带有newInstance方法的抽象工厂设计模式,例如:

public static FragmentOne newInstance(Bundle b) {
    FragmentOne fragment = new FragmentOne();
    fragment.setArguments(b);

    return fragment;
}

然后创建您的片段,如:

FragmentOne fragobj = FragmentOne.newInstance(bundle);

在这一行中你要设置参数并创建它。

答案 2 :(得分:1)

这些行似乎有问题:

 FragmentOne fragobj = new FragmentOne();
fragobj.setArguments(bundle);
adapter.addFragment(new FragmentOne(), "ONE");

您正在调用add片段时创建一个新片段(不使用您用于设置参数的片段)

更改为:

 FragmentOne fragobj = new FragmentOne();
fragobj.setArguments(bundle);
adapter.addFragment(fragobj, "ONE");

答案 3 :(得分:0)

变化:

adapter.addFragment(new FragmentOne(), "ONE");

要:

adapter.addFragment(fragobj, "ONE");

您正在创建FragmentOne的新实例,但Bundle已设置为fragobj