我有一个片段,其中包含在 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)