我的应用程序在 MainActivity 中包含带有两个片段的 ViewPager。第一个片段包含 ListView。我从来没有看到过这个,但我在 Google Play 中看到了用户关于 RuntimeException 的报告:
java.lang.RuntimeException: Unable to resume activity {com.my.project/com.my.project.MainActivity}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { (has extras) }} to activity {com.my.project/com.my.project.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2120)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2135)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1668)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:875)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:633)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { (has extras) }} to activity {com.my.project/com.my.project.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.deliverResults(ActivityThread.java:2532)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2107)
... 12 more
Caused by: java.lang.NullPointerException
at com.my.project.FirstFragment.load(FirstFragment.java:551)
at com.my.project.MainActivity.onActivityResult(MainActivity.java:132)
at android.app.Activity.dispatchActivityResult(Activity.java:3908)
at android.app.ActivityThread.deliverResults(ActivityThread.java:2528)
... 13 more
这是来自 FirstFragment.java 的代码:
public class FirstFragment extends Fragment {
ArrayList<ListItemObject> listItems = null;
ArrayAdapter<ListItemObject> mAdapter;
void load() {
mAdapter.clear(); // I get error here, that is FirstFragment.java:551
...
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
listItems = new ArrayList<ListItemObject>();
mAdapter = new MyAdapter(getActivity(), listItems);
listView.setAdapter(mAdapter);
}
}
这就是 mAdapter 的所有出现。而且我无法理解,它如何导致 NullPointerException。我认为有时 Fragment1 会破坏和重新创建,但没有 onCreateView()。我与一位遇到此问题的用户交谈,他说我在设置中取消选中“开发人员选项”中的“不保留活动”后,此错误就会消失。不知道,能修吗?或者我需要说我的所有用户“取消选中此选项”?
在我的 NainActivity 中:
public class MainActivity extends FragmentActivity {
Vector<Fragment> fragments;
ViewPager viewPager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
viewPager = (ViewPager) findViewById(R.id.pager);
fragments = new Vector<Fragment>();
fragments.add(Fragment.instantiate(this, Fragment1.class.getName()));
fragments.add(Fragment.instantiate(this, Fragment2.class.getName()));
viewPager.setOffscreenPageLimit(fragments.size());
PagerAdapter pagerAdapter = new PagerAdapter(super.getSupportFragmentManager(), fragments);
viewPager.setAdapter(pagerAdapter);
viewPager.setCurrentItem(0);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode==1) {
((MainChat)fragments.get(0)).load();
}
}
class PagerAdapter extends FragmentPagerAdapter implements IconPagerAdapter {
private List<Fragment> fragments;
public PagerAdapter(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();
}
}
}