第二次显示ViewPager时出现NullPointerException

时间:2016-03-02 15:43:11

标签: java android nullpointerexception android-viewpager

我正在使用一个ViewPager片段,它有两个片段作为孩子。这很好用,但是当我用另一个片段替换ViewPager片段并用ViewPager片段替换这个片段时,我的应用程序崩溃了以下NullPointerException

java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference
    at android.support.v4.app.FragmentManagerImpl.getFragment(FragmentManager.java:667)
    at android.support.v4.app.FragmentStatePagerAdapter.restoreState(FragmentStatePagerAdapter.java:211)
    at android.support.v4.view.ViewPager.onRestoreInstanceState(ViewPager.java:1319)
    at android.view.View.dispatchRestoreInstanceState(View.java:13756)
    at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2888)
    at android.view.View.restoreHierarchyState(View.java:13734)
    at android.support.v4.app.Fragment.restoreViewState(Fragment.java:468)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1094)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248)
    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613)
    at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5294)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)

我的ViewPager片段由以下源代码组成,如果它们为null,则会实例化两个子片段。此外,ViewPagerAdapter实施已实例化并分配给ViewPager

public class ConnectionPasswordViewPagerFragment extends Fragment
{
    private ViewPager vpConnectionPassword;
    private ConnectionPasswordViewPagerAdapter paConnectionPassword;
    private ConnectionPasswordGeneratorMACAddress passwordGeneratorMACAddress;
    private ConnectionPasswordGeneratorSerialNumber passwordGeneratorSerialNumber;

    public ConnectionPasswordViewPagerFragment()
    {
    // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
    super.onCreate(savedInstanceState);

    if ((this.passwordGeneratorSerialNumber == null) && (this.passwordGeneratorMACAddress == null))
    {
        this.passwordGeneratorMACAddress = new ConnectionPasswordGeneratorMACAddress();
        this.passwordGeneratorSerialNumber = new ConnectionPasswordGeneratorSerialNumber();
    }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState)
    {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_connection_password_view_pager, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState)
    {
    super.onActivityCreated(savedInstanceState);

    /*if (this.paConnectionPassword == null)
    {*/
        this.paConnectionPassword = new ConnectionPasswordViewPagerAdapter(this.getChildFragmentManager(), this.getActivity(), this.passwordGeneratorMACAddress, this.passwordGeneratorSerialNumber);
    //}
    this.vpConnectionPassword = (ViewPager) this.getActivity().findViewById(R.id.vpConnectionPassword);
    this.vpConnectionPassword.setAdapter(this.paConnectionPassword);
    }
}

下面显示的ViewPagerAdapter实现会在ArrayList中保留对子片段的引用,并实现metod getItem()getCount()以及getPageTitle()。类ViewPagerFragment只是扩展了支持Fragment类,并提供了由子片段实现的抽象方法getPageTitleStringID()

public class ConnectionPasswordViewPagerAdapter extends FragmentStatePagerAdapter
{
    private static final byte NUMBER_OF_FRAGMENTS = (byte) 2;

    private ArrayList<ViewPagerFragment> childFragments;
    private Activity displayActivity;

    public ConnectionPasswordViewPagerAdapter(FragmentManager fragmentMgm, Activity displayAct, ConnectionPasswordGeneratorMACAddress generatorMACAddress, ConnectionPasswordGeneratorSerialNumber generatorSerialNumber)
    {
    super(fragmentMgm);

    this.childFragments = new ArrayList<>(ConnectionPasswordViewPagerAdapter.NUMBER_OF_FRAGMENTS);
    this.setDisplayActivity(displayAct);
    if (generatorMACAddress != null)
    {
        this.childFragments.add(generatorMACAddress);
    }
    if (generatorSerialNumber != null)
    {
        this.childFragments.add(generatorSerialNumber);
    }
    }

    public Activity getDisplayActivity()
    {
    return this.displayActivity;
    }

    public void setDisplayActivity(Activity value)
    {
    this.displayActivity = value;
    }

    @Override
    public Fragment getItem(int position)
    {
    return this.childFragments.get(position);
    }

    @Override
    public int getCount()
    {
    return ConnectionPasswordViewPagerAdapter.NUMBER_OF_FRAGMENTS;
    }

    @Override
    public CharSequence getPageTitle(int position)
    {
    return this.getDisplayActivity().getString(this.childFragments.get(position).getPageTitleStringID());
    }
}

感谢您的帮助!

3 个答案:

答案 0 :(得分:6)

我通过在每次调用支持片段管理器的replace方法时创建包含视图寻呼机的片段来解决问题。

public void onSwitchToConnectionPasswordGenerator(View clickedView)
{
    this.fragConnectionPassword = new ConnectionPasswordViewPagerFragment();
    this.getSupportFragmentManager().beginTransaction().replace(R.id.flFragmentContainer, this.fragConnectionPassword).commitAllowingStateLoss();
}

答案 1 :(得分:2)

我认为你的Viewpager在Fragment中,并且这个片段的setRetainInstance设置为true(当我尝试保留父片段时我也有这个错误)

答案 2 :(得分:0)

更好的方法是你可以检查FragmentManager以获得Fragment,如果没有,那么创建一个新的实例,如下所示:

FragmentManager manager = getSupportFragmentManager();
Fragment fragment = manager.findFragmentByTag("YourFragmentName");

if(fragment == null)
  fragment = new YourFragment();

FragmentTransaction fragmentTransaction = manager.beginTransaction();
fragmentTransaction.replace(Your_Fragmnet_Container_Layout_id, fragment, fragment.getClass().getSimpleName());
fragmentTransaction.commit;