今天在改写系统自带的viewpager+actionbar tab(fragment)的时候
系统一直在报null pointer exception
log里有这么一条,点一下
定位到这里
fragment有空指针错误
于是就去找定义语句
<span style="font-size:18px;">public class MainActivity extends Activity implements ActionBar.TabListener {
/**
* The {@link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a {@link FragmentPagerAdapter}
* derivative, which will keep every loaded fragment in memory. If this
* becomes too memory intensive, it may be best to switch to a
* {@link android.support.v13.app.FragmentStatePagerAdapter}.
*/
SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {@link ViewPager} that will host the section contents.
*/
ViewPager mViewPager;
ArrayList<Fragment> fragments=new ArrayList(); //未初始化会报null pointer exception
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Set up the action bar.
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
Fragment fragmentA = new TabA();
Fragment fragmentB = new TabB();
Fragment fragmentC = new TabC();
Fragment fragmentD = new TabD();
Fragment fragmentE = new TabE();
fragments.add(fragmentA);
fragments.add(fragmentB);
fragments.add(fragmentC);
fragments.add(fragmentD);
fragments.add(fragmentE);
mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager(),
fragments);</span>
原来是ArrayList<Fragment> fragments=new ArrayList<Fragment>();
写成了ArrayList<Fragment> fragment
没有初始化
OK了,问题解决,可以运行了