修改后的FragmentStatePagerAdapter会在前棒棒糖上抛出NullPointerException

时间:2015-12-04 02:41:17

标签: android android-fragments nullpointerexception fragmentstatepageradapter

我稍微修改了FragmentStatePagerAdapter,以便能够动态地向寻呼机添加页面(like on this blogpost here)。

问题是,我在棒棒糖前设备上的5个安装中大约有4个直接开始NullPointerException。有时安装工作正常,我可以按照我想要的方式重新启动应用程序,而不会获得任何NullPointerException。这是堆栈跟踪:

12-04 13:23:46.084 16637-16637/abc E/AndroidRuntime: FATAL EXCEPTION: main
                                                                   java.lang.NullPointerException
                                                                       at android.support.v4.app.FragmentStatePagerAdapter.instantiateItem(FragmentStatePagerAdapter.java:116)
                                                                       at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:870)
                                                                       at android.support.v4.view.ViewPager.populate(ViewPager.java:1020)
                                                                       at android.support.v4.view.ViewPager.populate(ViewPager.java:952)
                                                                       at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1474)
                                                                       at android.view.View.measure(View.java:15518)
                                                                       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
                                                                       at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
                                                                       at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
                                                                       at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
                                                                       at android.view.View.measure(View.java:15518)
                                                                       at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1489)
                                                                       at android.view.View.measure(View.java:15518)
                                                                       at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:681)
                                                                       at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
                                                                       at android.view.View.measure(View.java:15518)
                                                                       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
                                                                       at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
                                                                       at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
                                                                       at android.view.View.measure(View.java:15518)
                                                                       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
                                                                       at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
                                                                       at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
                                                                       at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
                                                                       at android.view.View.measure(View.java:15518)
                                                                       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
                                                                       at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
                                                                       at android.view.View.measure(View.java:15518)
                                                                       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
                                                                       at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
                                                                       at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
                                                                       at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
                                                                       at android.view.View.measure(View.java:15518)
                                                                       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
                                                                       at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
                                                                       at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176)
                                                                       at android.view.View.measure(View.java:15518)
                                                                       at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
                                                                       at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
                                                                       at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
                                                                       at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
                                                                       at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
                                                                       at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
                                                                       at android.view.Choreographer.doCallbacks(Choreographer.java:562)
                                                                       at android.view.Choreographer.doFrame(Choreographer.java:532)
                                                                       at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
                                                                       at android.os.Handler.handleCallback(Handler.java:725)
                                                                       at android.os.Handler.dispatchMessage(Handler.java:92)
                                                                       at android.os.Looper.loop(Looper.java:137)
                                                                       at android.app.ActivityThread.main(ActivityThread.java:5041)
                                                                       at java.lang.reflect.Method.invokeNative(Native Method)
                                                                       at java.lang.reflect.Method.invoke(Method.java:511)
                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
                                                                       at dalvik.system.NativeStart.main(Native Method)

我的适配器:

private class BannerPagerAdapter extends FragmentStatePagerAdapter {

    private ArrayList<WeakReference<Fragment>> registeredFragments = new ArrayList<>();

    public BannerPagerAdapter(FragmentManager fm) {

        super(fm);
        this.add(BannerFragment.getInstance(0)); // <- this is crashing
    }

    @Override
    public Fragment getItem(int position) {

        WeakReference<Fragment> weakReference = registeredFragments.get(position);
        return weakReference.get();
    }

    @Override
    public int getCount() {
        return registeredFragments.size();
    }

    @Override
    public int getItemPosition(Object object){
        return PagerAdapter.POSITION_NONE;
    }

    public void add(Fragment f){

        registeredFragments.add(new WeakReference<>(f));
        notifyDataSetChanged();
    }

    public void remove(int position){
        registeredFragments.remove(position);
        notifyDataSetChanged();
    }
}
构造函数中的

this.add(BannerFragment.getInstance(0));每次都会崩溃,我甚至无法捕获NullPointerException块中的try { } catch { }。 该行只添加了带标题的第一页,之后的所有页面都是从Web动态加载的。知道如何解决这个问题吗?

0 个答案:

没有答案