片段:-
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)
答案 0 :(得分:0)
您的数据列表可以作为构造函数的论点传递,即删除适配器中的setData方法并使构造函数为
Runas /user:domain\user "cmd /C echo Test > ""C:\Program Files\Install2AgentService\Install2AgentWinService.exe.config"""
答案 1 :(得分:0)
要简单地修复它,请在{strong> 0 --> 0.0
1 --> 0.1
2 --> 0.2
3 --> 0.3
4 --> 0.4
5 --> 0.5
6 --> 0.6
7 --> 0.7
8 --> 0.8
9 --> 0.9
500 --> 50.0
501 --> 50.1
502 --> 50.2
503 --> 50.3
504 --> 50.4
505 --> 50.5
506 --> 50.6
507 --> 50.7
508 --> 50.8
509 --> 50.9
1000 --> 100.0
1001 --> 100.1
1002 --> 100.2
1003 --> 100.3
1004 --> 100.4
1005 --> 100.5
1006 --> 100.6
1007 --> 100.7
1008 --> 100.8
1009 --> 100.9
之前执行viewPagerAdapter.setData(data);