I am using view pager for navigating between pages. I am using FragmentStatePagerAdapter. I can swipe in one direction with out any problems. But if i swipe back to the older one. Its getting crash. I am getting null pointer exception.
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)
And here is my Code:
@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;
}
}
Here is my Fragment class:
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)));
}
}
}
I found the solution.
Its happening because of FragmentStatePagerAdapter. In the fragment we need to detach the getChildFragmentManager.
So we need to add the below code after 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);
}
}
来源:https://stackoverflow.com/questions/22355058/view-pager-crash-null-pointer-exception-due-to-navigating-back-to-the-old-fragme