将String Array从容器Activity传递给片段

时间:2017-02-17 15:53:43

标签: java android string android-fragments nullpointerexception

我有一个活动,其中有一个片段,我必须从活动中传递String数组,当我通过以下代码执行此操作时出现null指针异常请帮助

Main2Activity.java

public class Main2Activity extends AppCompatActivity implements OnMapReadyCallback, AsyncResponse{

String[] check = {
        "KFC bundle",
        "Pizza Hut b",
        "Pizza Point b"
} ;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    Intent intent = getIntent();
    newString = intent.getLongExtra("user_id", newString);
    Bundle bundle = new Bundle();
    bundle.putStringArray("name", check);
    Log.i("okkkkk", "Name Put: " + check[1]);
    Deals deals = new Deals();
    deals.setArguments(bundle);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    // Create the adapter that will return a fragment for each of the three
    // primary sections of the activity.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);
    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(mViewPager);
    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
}
    public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        try {
            switch (position) {
                case 0: {
                    MapsFragment mapsActivity=new MapsFragment();
                    return mapsActivity;
                }
                case 1: {
                   Deals deals = new Deals();
                    return deals;
                }
            }
        }
        catch ( Exception e)
        {
            Log.i("okkk", "Exception 1 : " + e);
        }

Deals.java `

public class Deals extends Fragment {
     @Nullable
     @Override
  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup     container, @Nullable Bundle savedInstanceState) {
     View view            =LayoutInflater.from(getActivity()).inflate(R.layout.fragment_main2,null);
       //  bundle = this.getArguments();
        String[] name = getArguments().getStringArray("name");
        Log.i("okkkkk", "Name String: " + name);
       // String[] deal = getArguments().getStringArray("deal");

    ListView listview =(ListView)view.findViewById(R.id.DealsListView);
    customList adapter = new customList(getActivity(), web, imageId,deal);
    listview.setAdapter(adapter);
    return view;

}`


java.lang.NullPointerException
                                                                                       at app.com.example.saeed.fypephaseone.Deals.onCreateView(Deals.java:42)
                                                                                       at android.support.v4.app.Fragment.performCreateView(Fragment.java:2074)
                                                                                       at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
                                                                                       at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1286)
                                                                                       at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:758)
                                                                                       at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:1632)
                                                                                       at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:637)
                                                                                       at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:143)
                                                                                       at android.support.v4.view.ViewPager.populate(ViewPager.java:1235)
                                                                                       at android.support.v4.view.ViewPager.populate(ViewPager.java:1083)
                                                                                       at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1609)
                                                                                       at android.view.View.measure(View.java:16842)

1 个答案:

答案 0 :(得分:0)

您需要在deals的{​​{1}}函数中使用实例PagerAdapter,而不是创建一个没有getItem作为参数的新Fragment实例

1。)使您的bundle实例全局到班级

deals

2。)在public class Main2Activity extends AppCompatActivity implements OnMapReadyCallback, AsyncResponse{ String[] check = { "KFC bundle", "Pizza Hut b", "Pizza Point b" } ; Deals deals; @Override protected void onCreate(Bundle savedInstanceState) { // code deals = new Deals(); deals.setArguments(bundle); Toolbar toolbar = (Toolbar) findV // code } // code

中使用deals
getItem

或者你可以这样做

    @Override
    public Fragment getItem(int position) {

        switch (position) {
            case 0: {
                MapsFragment mapsActivity=new MapsFragment();
                return mapsActivity;
            }
            case 1: {
                return deals;
            }
       }
    return null;
}