为啥 ViewPager 方法中为 null?
Posted
技术标签:
【中文标题】为啥 ViewPager 方法中为 null?【英文标题】:Why is null in ViewPager method?为什么 ViewPager 方法中为 null? 【发布时间】:2021-01-12 18:16:06 【问题描述】:我有 ViewPager。每个 ViewPager 的页面都是 customView。在addOnPageChangeListener(new ViewPager.OnPageChangeListener()
方法的自定义视图类中,我得到空值。我不明白为什么,因为在 viewPager - setOnClickListener(new OnClickListener()
的其他方法中没有空值。这个方法有效。
我的自定义视图:
public class CustomView extends LinearLayout
private LayoutInflater mInflater;
private Button button;
private ViewPager viewPager;
public void setPager(ViewPager viewPager)
this.viewPager = viewPager;
public CustomView(@NonNull Context context)
super(context);
public CustomView(@NonNull Context context, @Nullable AttributeSet attrs)
super(context, attrs);
mInflater = LayoutInflater.from(context);
View v = mInflater.inflate(R.layout.custom_view, this, true);
button = v.findViewById(R.id.nextButton);
button.setOnClickListener(new OnClickListener()
@Override
public void onClick(View v)
viewPager.setCurrentItem(viewPager.getCurrentItem() + 1, true);
);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener()
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
@Override
public void onPageSelected(int position)
@Override
public void onPageScrollStateChanged(int state)
);
和活动类:
public class Activity extends AppCompatActivity
private ViewPager viewPager;
private CustomPagerAdapter customPagerAdapter;
private CustomView customView;
protected void onCreate(@Nullable Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
customPagerAdapter = new CustomPagerAdapter(this);
viewPager = findViewById(R.id.viewPager);
customView = findViewById(R.id.customView);
viewPager.setAdapter(customPagerAdapter);
customView.setPager(viewPager);
我得到错误:
E/androidRuntime: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.viewpager.widget.ViewPager.addOnPageChangeListener(androidx.viewpager.widget.ViewPager$OnPageChangeListener)' on a null object reference
at CustomView.<init>(line : viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener())
【问题讨论】:
【参考方案1】:您在 CustomView 的构造函数中调用了addOnPageChangeListener
,并且尚未在您的视图中设置 viewpager。尝试在 setPager 方法中调用 addOnPageChangeListener
。
public class CustomView extends LinearLayout
private LayoutInflater mInflater;
private Button button;
private ViewPager viewPager;
public void setPager(ViewPager viewPager)
this.viewPager = viewPager;
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener()
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
@Override
public void onPageSelected(int position)
@Override
public void onPageScrollStateChanged(int state)
);
public CustomView(@NonNull Context context)
super(context);
public CustomView(@NonNull Context context, @Nullable AttributeSet attrs)
super(context, attrs);
mInflater = LayoutInflater.from(context);
View v = mInflater.inflate(R.layout.custom_view, this, true);
button = v.findViewById(R.id.nextButton);
button.setOnClickListener(new OnClickListener()
@Override
public void onClick(View v)
viewPager.setCurrentItem(viewPager.getCurrentItem() + 1, true);
);
【讨论】:
以上是关于为啥 ViewPager 方法中为 null?的主要内容,如果未能解决你的问题,请参考以下文章
Android getParentFragment() 在 Fragment 内的 ViewPager 中返回 null