为什么 ViewPager 方法中为 null?
Why is null in ViewPager method?
我有 ViewPager。每个 ViewPager 的页面都是 customView。在 addOnPageChangeListener(new ViewPager.OnPageChangeListener()
方法中的自定义视图 class 中,我得到 null。我不明白为什么,因为在 viewPager - setOnClickListener(new OnClickListener()
的其他方法中没有 null。这个方法有效。
我的自定义视图:
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) {
}
});
和activity class:
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())
您正在 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。每个 ViewPager 的页面都是 customView。在 addOnPageChangeListener(new ViewPager.OnPageChangeListener()
方法中的自定义视图 class 中,我得到 null。我不明白为什么,因为在 viewPager - setOnClickListener(new OnClickListener()
的其他方法中没有 null。这个方法有效。
我的自定义视图:
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) {
}
});
和activity class:
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())
您正在 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);
}
});
}