0

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

1 回答 1

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

于 2013-06-07T19:49:54.187 回答