【android】null pointer exception

今天在改写系统自带的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了,问题解决,可以运行了







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值