SetAdapter 空指针对象异常
Posted
技术标签:
【中文标题】SetAdapter 空指针对象异常【英文标题】:SetAdapter Null pointer Object Exception 【发布时间】:2021-04-22 21:46:25 【问题描述】:package com.example.shift.adapter;
import android.graphics.Color;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;
import com.example.shift.R;
import com.example.shift.modal.HomePageModal;
import com.example.shift.modal.HorizontalProductScrollModel;
import com.example.shift.modal.sliderModel;
import org.jetbrains.annotations.NotNull;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
public class HomePageAdapter extends RecyclerView.Adapter
private List<HomePageModal> homePageModalList;
public HomePageAdapter(List<HomePageModal> homePageModalList)
this.homePageModalList = homePageModalList;
@Override
public int getItemViewType(int position)
switch (homePageModalList.get(position).getType())
case 0:
return HomePageModal.BANNER_SLIDER;
case 1:
return HomePageModal.STRIP_AD_BANNER;
case 2:
return HomePageModal.HORIZONTAL_PRODUCT_VIEW;
case 3:
return HomePageModal.GRID_PRODUCT_VIEW;
default:
return -1;
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
switch (viewType)
case HomePageModal.BANNER_SLIDER:
View bannerSliderview = LayoutInflater.from(parent.getContext()).inflate(R.layout.slider_layout, parent, false);
return new BannerSliderViewHolder(bannerSliderview);
case HomePageModal.STRIP_AD_BANNER:
View stripAdview = LayoutInflater.from(parent.getContext()).inflate(R.layout.strip_ad_layout, parent, false);
return new StripAdBannerViewHolder(stripAdview);
case HomePageModal.HORIZONTAL_PRODUCT_VIEW:
View horizontalProductView = LayoutInflater.from(parent.getContext()).inflate(R.layout.horizontal_scroll_layout, parent, false);
return new HorizontalProductViewholder (horizontalProductView);
case HomePageModal.GRID_PRODUCT_VIEW:
View gridProductView = LayoutInflater.from(parent.getContext()).inflate(R.layout.grid_product_layout, parent, false);
return new GridProductViewholder(gridProductView);
default:
return null;
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position)
switch (homePageModalList.get(position).getType())
case HomePageModal.BANNER_SLIDER:
List<sliderModel> sliderModelList = homePageModalList.get(position).getSliderModelList();
((BannerSliderViewHolder) holder).setBannersliderViewpager(sliderModelList);
break;
case HomePageModal.STRIP_AD_BANNER:
int resource = homePageModalList.get(position).getResource();
String color = homePageModalList.get(position).getBackgroundColor();
((StripAdBannerViewHolder) holder).setStripAd(resource, color);
break;
case HomePageModal.HORIZONTAL_PRODUCT_VIEW:
String HorizontalLayouttitle =homePageModalList.get(position).getTitle();
List<HorizontalProductScrollModel>horizontalProductScrollModelList =homePageModalList.get(position).getHorizontalProductScrollModelList();
((HorizontalProductViewholder) holder).setHorizontalProductLayout(horizontalProductScrollModelList,HorizontalLayouttitle);
break;
case HomePageModal.GRID_PRODUCT_VIEW:
String GridLayouttitle =homePageModalList.get(position).getTitle();
List<HorizontalProductScrollModel>gridProductScrollModelList =homePageModalList.get(position).getHorizontalProductScrollModelList();
((GridProductViewholder) holder).setGridLayout(gridProductScrollModelList,GridLayouttitle);
break;
default:
return;
@Override
public int getItemCount()
return homePageModalList.size();
/// Slider List
public class BannerSliderViewHolder extends RecyclerView.ViewHolder
private ViewPager bannersliderViewpager;
private int currentpage = 2;
private Timer timer;
private final long DELAY_TIME = 3000;
private final long PERIOD_TIME = 3000;
public BannerSliderViewHolder(@NonNull View itemView)
super(itemView);
bannersliderViewpager = itemView.findViewById(R.id.banner_slider_viewpager);
private void setBannersliderViewpager(final List<sliderModel>sliderModelList)
SliderAdapter sliderAdapter = new SliderAdapter(sliderModelList);
bannersliderViewpager.setAdapter(sliderAdapter); **I am getting error at this line **
bannersliderViewpager.setClipToPadding(false);
bannersliderViewpager.setPageMargin(20);
bannersliderViewpager.setCurrentItem(currentpage);
ViewPager.OnPageChangeListener onPageChangeListener = new ViewPager.OnPageChangeListener()
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
@Override
public void onPageSelected(int position)
currentpage = position;
@Override
public void onPageScrollStateChanged(int state)
if(state == ViewPager.SCROLL_STATE_IDLE)
PageLooper(sliderModelList);
;
bannersliderViewpager.addOnPageChangeListener(onPageChangeListener);
startBannerSlideshow(sliderModelList);
bannersliderViewpager.setOnTouchListener(new View.OnTouchListener()
@Override
public boolean onTouch(View v, MotionEvent event)
PageLooper(sliderModelList);
stopBannerSlideshow();
if(event.getAction()==MotionEvent.ACTION_UP)
startBannerSlideshow(sliderModelList);
return false;
);
private void PageLooper(@NotNull List<sliderModel> sliderModelList)
if (currentpage == sliderModelList.size() - 2)
currentpage = 2;
bannersliderViewpager.setCurrentItem(currentpage, false);
if (currentpage == 1)
currentpage = sliderModelList.size() - 3;
bannersliderViewpager.setCurrentItem(currentpage, false);
private void startBannerSlideshow(List<sliderModel> sliderModelList)
Handler handler = new Handler();
Runnable update = new Runnable()
@Override
public void run()
if (currentpage >= sliderModelList.size())
currentpage = 1;
bannersliderViewpager.setCurrentItem(currentpage++, true);
;
timer = new Timer();
timer.schedule(new TimerTask()
@Override
public void run()
handler.post(update);
, DELAY_TIME, PERIOD_TIME);
private void stopBannerSlideshow()
timer.cancel();
/// Strip Ad
public class StripAdBannerViewHolder extends RecyclerView.ViewHolder
private ImageView stripAdImage;
private ConstraintLayout stripAdContainer;
public StripAdBannerViewHolder(@NonNull View itemView)
super(itemView);
stripAdImage = itemView.findViewById(R.id.strip_ad_image);
stripAdContainer = itemView.findViewById(R.id.strip_ad_container);
private void setStripAd(int resource, String color)
stripAdImage.setImageResource(resource);
stripAdContainer.setBackgroundColor(Color.parseColor(color));
/// Horizontal
public class HorizontalProductViewholder extends RecyclerView.ViewHolder
private TextView horizontalLayoutTitle;
private Button horizontalviewAllbtn;
private RecyclerView horizontalrecyclerview;
public HorizontalProductViewholder(@NonNull View itemView)
super(itemView);
horizontalLayoutTitle = itemView.findViewById(R.id.h_s_product_titile);
horizontalviewAllbtn = itemView.findViewById(R.id.horizontal_scroll_lt_viewAllbtn);
horizontalrecyclerview = itemView.findViewById(R.id.horizontal_scroll_lt_recyclerview);
private void setHorizontalProductLayout(@NotNull List<HorizontalProductScrollModel> horizontalProductScrollModelList, String title)
horizontalLayoutTitle.setText(title);
if(horizontalProductScrollModelList.size() >8 )
horizontalviewAllbtn.setVisibility(View.VISIBLE);
else
horizontalviewAllbtn.setVisibility(View.INVISIBLE);
HorizontalProductScrollAdapter horizontalProductScrollAdapter = new HorizontalProductScrollAdapter(horizontalProductScrollModelList);
LinearLayoutManager linearLayoutManagerhorizontal = new LinearLayoutManager(itemView.getContext());
linearLayoutManagerhorizontal.setOrientation(LinearLayoutManager.HORIZONTAL);
horizontalrecyclerview.setLayoutManager(linearLayoutManagerhorizontal);
horizontalrecyclerview.setAdapter(horizontalProductScrollAdapter);
horizontalProductScrollAdapter.notifyDataSetChanged();
/// Grid Layout
public class GridProductViewholder extends RecyclerView.ViewHolder
private TextView gridLayoutTitle;
private Button gridLayoutViewAllbtn;
private GridView gridView;
public GridProductViewholder(@NonNull View itemView)
super(itemView);
TextView gridLayoutTitle = itemView.findViewById(R.id.grid_product_layout_titile);
Button gridLayoutViewAllbtn = itemView.findViewById(R.id.grid_product_layout_viewAlbtn);
GridView gridView = itemView.findViewById(R.id.grid_product_layout_gridView);
private void setGridLayout(List<HorizontalProductScrollModel>horizontalProductScrollAdapterList,String title)
gridLayoutTitle.setText(title);
gridView.setAdapter(new gridProductLayoutAdapter(horizontalProductScrollAdapterList));
[Null object Exception][1]**我收到此错误 java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.viewpager.widget.ViewPager.setAdapter(androidx.viewpager.widget.PagerAdapter)'在空对象引用上 在 com.example.shift.adapter.HomePageAdapter$BannerSliderViewHolder.setBannersliderViewpager(HomePageAdapter.java:124) **
【问题讨论】:
【参考方案1】:bannersliderViewpager
是使用 itemView.findViewById(R.id.banner_slider_viewpager);
初始化的,当没有具有给定 ID 的视图时,它会返回 null
。这会导致您的 NullPointerException
,因为您正在调用 null
对象引用上的方法。
【讨论】:
以上是关于SetAdapter 空指针对象异常的主要内容,如果未能解决你的问题,请参考以下文章