我的oncreate看起来像:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_score);
tbar = (android.support.v7.widget.Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(tbar);
tabHost = (MaterialTabHost) this.findViewById(R.id.materialTabHost);
pager = (ViewPager) this.findViewById(R.id.pager );
// init view pager
adapter = new CustomAdapter(getSupportFragmentManager());
pager.setAdapter(adapter);
pager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// when user do a swipe the selected tab change
tabHost.setSelectedNavigationItem(position);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {}
@Override
public void onPageScrollStateChanged(int state) {}
});
for (int i = 0; i < adapter.getCount(); i++) {
tabHost.addTab(
tabHost.newTab()
.setText(adapter.getPageTitle(i))
.setTabListener(this)
);
}
}
当我尝试在viewpager中滚动时,出现如下错误:
FATAL EXCEPTION: main
java.lang.NullPointerException
at android.support.v7.widget.RecyclerView.onInterceptTouchEvent(RecyclerView.java:1819)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1839)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2259)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1922)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2259) ....
任何帮助???