我有一个带有GridLayoutManager的recyclerview,在项目中我使用ViewPager来显示照片,我在下面发生了崩溃:
02-28 14:58:56.618 30212-30212 / ofeksprojects.ofek.com.nightout E / AndroidRuntime:FATAL EXCEPTION:main 处理:ofeksprojects.ofek.com.nightout,PID:30212 java.lang.NullPointerException:尝试调用虚方法' android.view.View android.widget.LinearLayout.getChildAt(int)'在null对象引用上 在eu.fiskur.simpleviewpager.SimpleViewPager.setIndicator(SimpleViewPager.java:217) 在eu.fiskur.simpleviewpager.SimpleViewPager.onLayout(SimpleViewPager.java:243) 在android.view.View.layout(View.java:16694) 在android.view.ViewGroup.layout(ViewGroup.java:5481) 在android.support.constraint.ConstraintLayout.onLayout(ConstraintLayout.java:1495) 在android.view.View.layout(View.java:16694) 在android.view.ViewGroup.layout(ViewGroup.java:5481) 在android.widget.FrameLayout.layoutChildren(FrameLayout.java:336) 在android.widget.FrameLayout.onLayout(FrameLayout.java:273) 在android.view.View.layout(View.java:16694) 在android.view.ViewGroup.layout(ViewGroup.java:5481) 在android.support.v7.widget.RecyclerView $ LayoutManager.layoutDecoratedWithMargins(RecyclerView.java:8994) 在android.support.v7.widget.GridLayoutManager.layoutChunk(GridLayoutManager.java:685) 在android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1518) 在android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:585) 在android.support.v7.widget.GridLayoutManager.onLayoutChildren(GridLayoutManager.java:170) 在android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3719) 在android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3436) 在android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3988) 在android.view.View.layout(View.java:16694) 在android.view.ViewGroup.layout(ViewGroup.java:5481) 在android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079) 在android.view.View.layout(View.java:16694) 在android.view.ViewGroup.layout(ViewGroup.java:5481) 在android.widget.FrameLayout.layoutChildren(FrameLayout.java:336) 在android.widget.FrameLayout.onLayout(FrameLayout.java:273) 在android.view.View.layout(View.java:16694) 在android.view.ViewGroup.layout(ViewGroup.java:5481) 在android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079) 在android.view.View.layout(View.java:16694) 在android.view.ViewGroup.layout(ViewGroup.java:5481) 在android.support.design.widget.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:132) 在android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:42) 在android.support.design.widget.AppBarLayout $ ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1361) 在android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:874) 在android.view.View.layout(View.java:16694) 在android.view.ViewGroup.layout(ViewGroup.java:5481) 在android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1172) 在android.view.View.layout(View.java:16694) 在android.view.ViewGroup.layout(ViewGroup.java:5481) 在android.widget.FrameLayout.layoutChildren(FrameLayout.java:336) 在android.widget.FrameLayout.onLayout(FrameLayout.java:273) 在android.view.View.layout(View.java:16694) 在android.view.ViewGroup.layout(ViewGroup.java:5481) 在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743) 在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586) 在android.widget.LinearLayout.onLayout(LinearLayout.java:1495) 在android.view.View.layout(View.java:16694) 在android.view.ViewGroup.layout(ViewGroup.java:5481) 在android.widget.FrameLayout.layoutChildren(FrameLayout.java:336) 在android.widget.FrameLayout.onLayout(FrameLayout.java:273) 在android.view.View.layout(View.java:16694) 在android.view.ViewGroup.layout(ViewGroup.java:5481) 在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743) 在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586) 在android.widget.LinearLayout.onLayout(LinearLayout.java:1495) 在android.view.View.layout(View.java:16694) 在android.view.ViewGroup.layout(ViewGroup.java:5481
我记得我读过它是一个已知的问题,因为recyclerview和viewpager都是水平滚动但我无法找到解决方案。谁可以对它有所了解?
这是代码 -
ublic class PlacesSearchAdapter extends BaseQuickAdapter<Place,PlacesSearchAdapter.PlaceViewHolder> {
private static String PLACE_PHOTOS_BASE_URL = "https://maps.googleapis.com/maps/api/place/photo?";
public PlacesSearchAdapter(@Nullable List<Place> data) {
super(R.layout.place_item_base_layout,data);
}
@Override
protected void convert(PlaceViewHolder helper, Place item) {
helper.nameTV.setText(item.getName());
helper.addressTV.setText(item.getTextualAddress());
loadThumbs(helper,item);
}
private void loadThumbs(PlaceViewHolder helper, Place item) {
ArrayList<String> photosUrl = new ArrayList<>();
for (Place.PlacePhoto photo:item.getPhotos()) {
StringBuilder builder = new StringBuilder(PLACE_PHOTOS_BASE_URL);
builder.append("maxwidth="+photo.getWidth());
builder.append("&");
builder.append("maxheight="+photo.getHeight());
builder.append("&");
builder.append("photoreference="+photo.getReference());
photosUrl.add(builder.toString());
}
String[] photosArr = photosUrl.toArray(new String[photosUrl.size()]);
helper.photosPager.setImageUrls(photosArr, new ImageURLLoader() {
@Override
public void loadImage(ImageView imageView, String url) {
Picasso.with(imageView.getContext()).load(url).fit().into(imageView);
}
});
}
public class PlaceViewHolder extends BaseViewHolder {
SimpleViewPager photosPager;
TextView nameTV,addressTV;
MaterialFavoriteButton favoriteButton;
public PlaceViewHolder(View view) {
super(view);
nameTV = view.findViewById(R.id.nameTV_placeAdapter);
addressTV = view.findViewById(R.id.addressTV_placeAdapter);
photosPager = view.findViewById(R.id.placeThumb_placeAdapter);
int indicatorColor = Color.parseColor("#ffffff");
int selectedIndicatorColor = Color.parseColor("#fff000");
photosPager.showIndicator(indicatorColor, selectedIndicatorColor);
favoriteButton = view.findViewById(R.id.favoriteButton_placeAdapter);
}
}
}
和我的onViewCreated() -
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
final CardView searchCard = view.findViewById(R.id.searchCard_searchFrag);
placesRV = view.findViewById(R.id.resultList_searchFrag);
placesRV.setLayoutManager(new GridLayoutManager(placesRV.getContext(),1,GridLayoutManager.HORIZONTAL,false));
assert getActivity()!=null;
placesRV.setNestedScrollingEnabled(true);
placesRV.getLayoutManager().setAutoMeasureEnabled(true);