我的片段中有ViewPager
,并且我正尝试从服务器加载ViewPager
中的图像。已成功从Logcat的服务器中获取图像,但无法在视图分页器中显示,并且应用崩溃。
它在日志猫中显示以下错误:
java.lang.NullPointerException: Argument must not be null
at com.bumptech.glide.util.Preconditions.checkNotNull(Preconditions.java:31)
at com.bumptech.glide.util.Preconditions.checkNotNull(Preconditions.java:25)
at com.bumptech.glide.RequestBuilder.into(RequestBuilder.java:675)
at Adapters.HomeOfferAdapter.instantiateItem(HomeOfferAdapter.java:57)
at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:1010)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1158)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1092)
at android.support.v4.view.ViewPager.setAdapter(ViewPager.java:540)
at com.app.bookbudiapp.Home$4$1.run(Home.java:371)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:7000)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:441)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)
这是我的下面的代码:
HomeOfferAdapter.java
public class HomeOfferAdapter extends PagerAdapter {
private Context context;
private LayoutInflater layoutInflater;
private List<HomeOffersModel> banners;
public HomeOfferAdapter(List<HomeOffersModel> banners,Context context){
this.banners = banners;
this.context = context;
}
@Override
public int getCount() {
return banners.size();
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
return view == o;
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
assert layoutInflater != null;
View view = layoutInflater.inflate(R.layout.onboard_frag,container,false);
ImageView slideImage = view.findViewById(R.id.offerImage);
HomeOffersModel model = banners.get(position);
RequestOptions options = new RequestOptions();
options.placeholder(R.drawable.openbook);
Glide.with(context).load(model.getOffers()).apply(options).into(slideImage);
container.addView(view);
return view;
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView((LinearLayout)object);
}
}
Home.java
public class Home extends Fragment {
ViewPager homeOffers;
HomeOfferAdapter offerAdapter;
List<HomeOffersModel> banners;
private static final String HOME_OFFERS = "https://example.com";
public Home() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_home, container, false);
homeOffers = view.findViewById(R.id.homeOffers);
banners = new ArrayList<>();
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(22,TimeUnit.SECONDS)
.readTimeout(22,TimeUnit.SECONDS)
.writeTimeout(22,TimeUnit.SECONDS)
.build();
Request request = new Request.Builder().url(HOME_OFFERS).build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onResponse(Call call, final Response response) throws IOException {
if(getActivity() != null) {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
try {
JSONArray jsonArray = new JSONArray(response.body().string());
for (int i=0 ; i<jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String str = jsonObject.getString("Image");
Log.d("response",str);
HomeOffersModel model = new HomeOffersModel(str);
banners.add(model);
}
offerAdapter = new HomeOfferAdapter(banners, getActivity());
homeOffers.setAdapter(offerAdapter);
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
response.close();
}
}
});
}
}
@Override
public void onFailure(Call call, final IOException e) {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
TastyToast.makeText(getActivity(),e.getMessage(),TastyToast.LENGTH_SHORT,TastyToast.ERROR).show();
}
});
}
});
return view;
}
}
HomeOffersModel.java
public class HomeOffersModel {
String offers;
public HomeOffersModel(){
}
public HomeOffersModel(String offers){
this.offers = offers;
}
public String getOffers() {
return offers;
}
public void setOffers(String offers) {
this.offers = offers;
}
}
有人请让我知道为什么我已经正确设置了适配器,但还是收到上述错误。 任何帮助将不胜感激。
谢谢