Attempt to invoke virtual method void android.widget.AdapterView.setAdapter(android.widget.Adapter)

13.8k views Asked by At

I want to use listview in viewpager.

Here is my code:

My Fragment: CampaignsFragment

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    // BEGIN_INCLUDE (setup_viewpager)
    // Get the ViewPager and set it's PagerAdapter so that it can display items
    mViewPager = (ViewPager) view.findViewById(R.id.viewpager_campaigns);
    mViewPager.setAdapter(new CampaignsAdapter(getFragmentManager()));
    // END_INCLUDE (setup_viewpager)


    // BEGIN_INCLUDE (setup_slidingtablayout)
    // Give the SlidingTabLayout the ViewPager, this must be done AFTER the ViewPager has had
    // it's PagerAdapter set.
    mSlidingTabLayout = (SlidingTabLayout) view.findViewById(R.id.sliding_tabs_campaigns);
    mSlidingTabLayout.setViewPager(mViewPager);
    // END_INCLUDE (setup_slidingtablayout)
}

My adapter: CampaignsAdapter

public class CampaignsAdapter extends FragmentPagerAdapter {

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

    /**
     * @return the number of pages to display
     */
    @Override
    public int getCount() {
        return 2;
    }

    @Override
    public Fragment getItem(int position) {
        String[] campaigns_type = {"active", "previous"};

        //Log.w(String.valueOf(position), String.valueOf(position));
        return CampaignsListFragment.newInstance(campaigns_type[position]);
    }
}

This is my next fragment: CampaignsListFragment

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (getArguments() != null) {
        mCampaignType = getArguments().getString(ARG_CAMPAIGN_TYPE);
    }

    // TODO: Change Adapter to display your content
    mAdapter = new ArrayAdapter<DummyContent.DummyItem>(getActivity(),
            android.R.layout.simple_list_item_1, android.R.id.text1, DummyContent.ITEMS);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_campaigns_list, container, false);

    // Set the adapter
    mListView = (AbsListView) view.findViewById(android.R.id.list);
    ((AdapterView<ListAdapter>) mListView).setAdapter(mAdapter);

    // Set OnItemClickListener so we can be notified on item clicks
    mListView.setOnItemClickListener(this);

    return view;
}

But I faced problem:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.AdapterView.setAdapter(android.widget.Adapter)' on a null object reference at com.safaroff.bakcellroaming.CampaignsListFragment.onCreateView(CampaignsListFragment.java:83)

enter image description here

enter image description here

What's problem?

1

There are 1 answers

0
Zulfugar Ismayilzadeh On BEST ANSWER

I've solved my problem. Problem was here:

mListView = (AbsListView) view.findViewById(android.R.id.list);

I've changed to this.

mListView = (AbsListView) view.findViewById(R.id.listview_campaigns);