查看Pager Crash Null Pointer由于导航回旧片段而异常 - Android

时间:2014-03-12 14:44:53

标签: android android-fragments android-viewpager fragmentstatepageradapter

我正在使用视图寻呼机在页面之间导航。我正在使用FragmentStatePagerAdapter。我可以向一个方向滑动而没有任何问题。但是,如果我再刷回旧的那个。它崩溃了。我得到空指针异常。

03-12 10:20:34.273: E/AndroidRuntime(29035): Process: com.titlesource.notarymobile, PID: 29035
03-12 10:20:34.273: E/AndroidRuntime(29035): java.lang.NullPointerException
03-12 10:20:34.273: E/AndroidRuntime(29035):    at android.support.v4.app.Fragment.instantiate(Fragment.java:399)
03-12 10:20:34.273: E/AndroidRuntime(29035):    at android.support.v4.app.FragmentState.instantiate(Fragment.java:97)
03-12 10:20:34.273: E/AndroidRuntime(29035):    at android.support.v4.app.FragmentManagerImpl.restoreAllState(FragmentManager.java:1790)
03-12 10:20:34.273: E/AndroidRuntime(29035):    at android.support.v4.app.Fragment.performCreate(Fragment.java:1489)
03-12 10:20:34.273: E/AndroidRuntime(29035):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:893)
03-12 10:20:34.273: E/AndroidRuntime(29035):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
03-12 10:20:34.273: E/AndroidRuntime(29035):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
03-12 10:20:34.273: E/AndroidRuntime(29035):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
03-12 10:20:34.273: E/AndroidRuntime(29035):    at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
03-12 10:20:34.273: E/AndroidRuntime(29035):    at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163)
03-12 10:20:34.273: E/AndroidRuntime(29035):    at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
03-12 10:20:34.273: E/AndroidRuntime(29035):    at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
03-12 10:20:34.273: E/AndroidRuntime(29035):    at android.support.v4.view.ViewPager$3.run(ViewPager.java:244)
03-12 10:20:34.273: E/AndroidRuntime(29035):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
03-12 10:20:34.273: E/AndroidRuntime(29035):    at android.view.Choreographer.doCallbacks(Choreographer.java:574)
03-12 10:20:34.273: E/AndroidRuntime(29035):    at android.view.Choreographer.doFrame(Choreographer.java:543)
03-12 10:20:34.273: E/AndroidRuntime(29035):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
03-12 10:20:34.273: E/AndroidRuntime(29035):    at android.os.Handler.handleCallback(Handler.java:733)
03-12 10:20:34.273: E/AndroidRuntime(29035):    at android.os.Handler.dispatchMessage(Handler.java:95)
03-12 10:20:34.273: E/AndroidRuntime(29035):    at android.os.Looper.loop(Looper.java:136)
03-12 10:20:34.273: E/AndroidRuntime(29035):    at android.app.ActivityThread.main(ActivityThread.java:5102)
03-12 10:20:34.273: E/AndroidRuntime(29035):    at java.lang.reflect.Method.invokeNative(Native Method)
03-12 10:20:34.273: E/AndroidRuntime(29035):    at java.lang.reflect.Method.invoke(Method.java:515)
03-12 10:20:34.273: E/AndroidRuntime(29035):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-12 10:20:34.273: E/AndroidRuntime(29035):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-12 10:20:34.273: E/AndroidRuntime(29035):    at dalvik.system.NativeStart.main(Native Method)

这是我的代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_page_view);
    mProgressDialog = new ProgressDialog(JobDetailsActivity.this);
    mProgressDialog.setMessage("Loading");
    Bundle bundle = getIntent().getExtras();
    position = bundle.getInt("list_posiition_details");
        List<Fragment> fragments = getFragments();
        pageAdapter = new MyPageAdapter(getSupportFragmentManager(), fragments);

        pager = (ViewPager)findViewById(R.id.viewpager);
        pager.setAdapter(pageAdapter);
        pager.setCurrentItem(position);
        pager.setOffscreenPageLimit(3);
}


public interface YourFragmentInterface {
    void fragmentBecameVisible();
}

 private List<Fragment> getFragments(){
        List<Fragment> fList = new ArrayList<Fragment>();

        final ArrayList<?> temp = DataModel.getScheduleList();

         for (final Object obj : temp) {
             if(obj instanceof Job) {
                 String a = ((Job) obj).getA();
                 String b = Long.toString(((Job) obj).getB());
                 String c = ((Job) obj).getC();
                 fList.add(JobDetailsFragment_.newInstance(a, b, c));
                 mProgressDialog.dismiss();
             }
         }
        return fList;
    }


  public class MyPageAdapter extends FragmentStatePagerAdapter {
        private List<Fragment> fragments;

        public MyPageAdapter(FragmentManager fm, List<Fragment> fragments) {
            super(fm);
            this.fragments = fragments;
        }
        @Override
        public Fragment getItem(int position) {
            return this.fragments.get(position);
        }

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

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

    }

这是我的Fragment类:

public class MyFragment extends Fragment {
public static final String EXTRA_MESSAGE = "EXTRA_MESSAGE";


private static View view;
/**
 * Note that this may be null if the Google Play services APK is not
 * available.
 */

SupportMapFragment mSupportMapFragment;
private GoogleMap googleMap;

public static final MyFragment newInstance(String message)
{
    MyFragment f = new MyFragment();
    Bundle bdl = new Bundle(1);
    bdl.putString(EXTRA_MESSAGE, message);
    f.setArguments(bdl);
    return f;
}
//  View view;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
    Bundle savedInstanceState) {
    String message = getArguments().getString(EXTRA_MESSAGE);

     if (view != null) {
            ViewGroup parent = (ViewGroup) view.getParent();
            if (parent != null)
                parent.removeView(view);
        }
        try {
            view = inflater.inflate(R.layout.myfragment_layout, container, false);
            TextView messageTextView = (TextView)view.findViewById(R.id.textView);
            messageTextView.setText(message);
        } catch (InflateException e) {
            /* map is already there, just return view as it is */
        }

//      View v = inflater.inflate(R.layout.myfragment_layout, container, false);


    return view;
}


@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    FragmentManager fm = getChildFragmentManager();
    mSupportMapFragment = (SupportMapFragment) fm.findFragmentById(R.id.location_map);
    if (mSupportMapFragment == null) {
        mSupportMapFragment = SupportMapFragment.newInstance();
        fm.beginTransaction().replace(R.id.location_map, mSupportMapFragment).commit();
    }
}

@Override
public void onResume() {
    super.onResume();
    if (googleMap == null) {
        googleMap = mSupportMapFragment.getMap();
        googleMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)));
    }
  }

}

1 个答案:

答案 0 :(得分:8)

我找到了解决方案。

由于FragmentStatePagerAdapter而发生了这种情况。在片段中,我们需要分离getChildFragmentManager。

因此我们需要在activityCreated()

之后添加以下代码
        @Override
    public void onDetach() {
        super.onDetach();
        try {
            Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager");
            childFragmentManager.setAccessible(true);
            childFragmentManager.set(this, null);
        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }