我有一个片段,其中包含onResume中定义的地图视图,如下面的代码所示。当我退出片段然后回来或在不同的片段中移动并返回到这个地图片段时,我没有任何错误或问题。但是,当我从下面提供的按钮转到谷歌地图应用程序并回到此片段时,它会在findFragmentById(R.id.map)处给出NPE,而logcat会说“无法恢复活动”。
编辑:每当调用onCreateView时,它都可以工作,但是当调用onResume或onStart时它会崩溃。我以前弄错了。编辑。 公共类Fragment2扩展了Fragment {
...
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View rootView = inflater.inflate(R.layout.fragment_fragment2,
container, false);
return rootView;
}
@Override
public void onResume() {
// TODO Auto-generated method stub
super.onResume();
map = ((SupportMapFragment) getFragmentManager().findFragmentById
**NPE here** (R.id.map)).getMap();
...
}
public void onPause() {
// TODO Auto-generated method stub
super.onPause();
SupportMapFragment f = (SupportMapFragment) getFragmentManager()
.findFragmentById(R.id.map);
if (f != null)
getFragmentManager().beginTransaction().remove(f).commit();
}
这是logcat: -
06-07 21:32:43.295: E/AndroidRuntime(27112): FATAL EXCEPTION: main
06-07 21:32:43.295: E/AndroidRuntime(27112): java.lang.RuntimeException: Unable to resume activity {com.example.mapsapiv2demo/com.example.mapsapiv2demo.MainActivity}: java.lang.NullPointerException
06-07 21:32:43.295: E/AndroidRuntime(27112): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2869)
06-07 21:32:43.295: E/AndroidRuntime(27112): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2898)
06-07 21:32:43.295: E/AndroidRuntime(27112): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1289)
06-07 21:32:43.295: E/AndroidRuntime(27112): at android.os.Handler.dispatchMessage(Handler.java:99)
06-07 21:32:43.295: E/AndroidRuntime(27112): at android.os.Looper.loop(Looper.java:137)
06-07 21:32:43.295: E/AndroidRuntime(27112): at android.app.ActivityThread.main(ActivityThread.java:5226)
06-07 21:32:43.295: E/AndroidRuntime(27112): at java.lang.reflect.Method.invokeNative(Native Method)
06-07 21:32:43.295: E/AndroidRuntime(27112): at java.lang.reflect.Method.invoke(Method.java:511)
06-07 21:32:43.295: E/AndroidRuntime(27112): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
06-07 21:32:43.295: E/AndroidRuntime(27112): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
06-07 21:32:43.295: E/AndroidRuntime(27112): at dalvik.system.NativeStart.main(Native Method)
06-07 21:32:43.295: E/AndroidRuntime(27112): Caused by: java.lang.NullPointerException
06-07 21:32:43.295: E/AndroidRuntime(27112): at com.example.mapsapiv2demo.Fragment2.onResume(Fragment2.java:56)
06-07 21:32:43.295: E/AndroidRuntime(27112): at android.support.v4.app.Fragment.performResume(Fragment.java:1503)
06-07 21:32:43.295: E/AndroidRuntime(27112): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
06-07 21:32:43.295: E/AndroidRuntime(27112): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
06-07 21:32:43.295: E/AndroidRuntime(27112): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1070)
06-07 21:32:43.295: E/AndroidRuntime(27112): at android.support.v4.app.FragmentManagerImpl.dispatchResume(FragmentManager.java:1871)
06-07 21:32:43.295: E/AndroidRuntime(27112): at android.support.v4.app.FragmentActivity.onResumeFragments(FragmentActivity.java:455)
06-07 21:32:43.295: E/AndroidRuntime(27112): at android.support.v4.app.FragmentActivity.onPostResume(FragmentActivity.java:444)
06-07 21:32:43.295: E/AndroidRuntime(27112): at android.app.Activity.performResume(Activity.java:5195)
06-07 21:32:43.295: E/AndroidRuntime(27112): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2859)
答案 0 :(得分:0)
您正在移除onPause
中的子碎片,稍后调用onResume
时,它不存在,因此NPE的原因。
另一件事是你不应该在xml中放入子碎片。始终从代码中添加它们。
另一件事是,在将片段添加到片段时,您应该使用getChildFragmentManager
而不是getFragmentManager
。
在这里查看代码:http://code.google.com/p/gmaps-api-issues/issues/detail?id=5064#c1作为示例。
修改强>
注意:当布局包含
<fragment>
时,您无法将布局扩展为片段。只有动态添加到片段时才支持嵌套片段。
来自:http://developer.android.com/about/versions/android-4.2.html#NestedFragments