分段:-

public class HomeProductFragment extends Fragment implements ActionBar.TabListener {

    private List<String> data = new ArrayList<>();

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.fragment_home_product, container, false);


        userService = ApiUtils.getUserService();
        Call<ProductCategoryDetails> callCategory = userService.getProductCategoryJSON();
        callCategory.enqueue(new Callback<ProductCategoryDetails>() {
            @Override
            public void onResponse(Call<ProductCategoryDetails> call, Response<ProductCategoryDetails> response) {
                try {
                    if (response.body() != null && response.body().getData() != null) {
                        for(int j = 0;j<response.body().getData().getCategories().size();j++) {
                            productCategory = response.body().getData().getCategories().get(j).getMainCategory();
                            data.add(productCategory);
                        }
                        Log.d("SET321","==set=="+data);
                        viewPager = (ViewPager) view.findViewById(R.id.viewpager);
                        viewPagerAdapter = new ViewPagerAdapter(getFragmentManager());
                        viewPager.setAdapter(viewPagerAdapter);
                        viewPagerAdapter.setData(data); <-----------

                    } else
                        Toast.makeText(getContext(), "Error!======" + response.code(), Toast.LENGTH_SHORT).show();
                }catch (Exception e){
                    Log.d("TEST123","Exception");
                }
            }

            @Override
            public void onFailure(Call<ProductCategoryDetails> call, Throwable t) {
                Log.d("TAG","FailedCat");
            }
        });


ViewPagerAdapter:-

private List<String> data;

    public ViewPagerAdapter(FragmentManager manager) {
        super(manager);
    }

    public void setData(List<String> data){
        this.data = data;
    }


    @Override
    public Fragment getItem(int position) {
        return TabFragment.getInstance(position);
    }

    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        String[] stockArr = new String[data.size()];
        return stockArr[position];
        //return data.get(position);

    }

    @Override
    public int getCount() {
        String[] stockArr = new String[data.size()];
        return stockArr.length;
        //return data.size();
    }

    public int getItemPosition(Object object) {
        String[] stockArr = new String[data.size()];
        return stockArr.length;
        //return data.size();
    }
}


错误:


  java.lang.NullPointerException:尝试调用接口方法
  空对象引用上的'int java.util.List.size()'
          在com.custom_adapters.ViewPagerAdapter.getCount(ViewPagerAdapter.java:43)

最佳答案

您的数据列表可以作为构造函数争论传递,即删除适配器中的setData方法并使构造函数成为

  ViewPagerAdapter(FragmentManager manager,List<String> data){
         super(manager);
         this.data = data;
    }

07-27 17:31