我有一个活动,其中有一个片段,当我通过以下代码执行此操作时,我必须从活动中传递字符串数组,发生空指针异常,请帮助

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)


最佳答案

您需要在dealsPagerAdapter函数中使用实例getItem,而不是创建一个没有bundle作为参数的新Fragment实例。

1.)使您的deals实例成为全局类

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


2.)在deals内使用getItem

    @Override
    public Fragment getItem(int position) {

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


或者你可以这样做

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

关于java - 当将字符串数组从容器Activity传递到 fragment 时,getArguments()返回null,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42302122/

10-08 21:54