Android TableLayout上出现空指针异常
在onCreate()之前声明了此项: 在onCreate()中:Android TableLayout上出现空指针异常,android,android-support-library,Android,Android Support Library,在onCreate()之前声明了此项: 在onCreate()中: 我很难看到侦听器是如何为空的。我还有其他NullPointerException,它们甚至在原语上声明null对象,包括可绘制PNG定义的int?您忘记将布局设置为setContentView() Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.design.widget.TabLa
我很难看到侦听器是如何为空的。我还有其他NullPointerException,它们甚至在原语上声明null对象,包括可绘制PNG定义的int?您忘记将布局设置为setContentView()
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.design.widget.TabLayout.addOnTabSelectedListener(android.support.design.widget.TabLayout$OnTabSelectedListener)' on a null object reference
TabLayout.OnTabSelectedListener listener;
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabLayout);
listener = new TabLayout.OnTabSelectedListener(){
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
};
tabLayout.addOnTabSelectedListener(listener);