我正在使用视图寻呼机在页面之间导航.我正在使用 FragmentStatePagerAdapter.我可以毫无问题地向一个方向滑动.但如果我刷回旧的.它正在崩溃.我收到空指针异常.
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)
这是我的代码:
@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;
}
}
这是我的片段类:
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)));
}
}
}
我找到了解决方案.
它的发生是因为 FragmentStatePagerAdapter.在片段中,我们需要分离 getChildFragmentManager.
Its happening because of FragmentStatePagerAdapter. In the fragment we need to detach the getChildFragmentManager.
所以我们需要在activityCreated()之后添加下面的代码
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);
}
}
这篇关于由于导航回旧片段,查看 Pager Crash Null Pointer Exception - Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!