片段删除和提交onPause()抛出异常

时间:2013-11-11 07:17:26

标签: android

在我的应用程序中,有一个包含三个选项卡(地图,列表,设置)的片段活动。我已经将删除和提交放在map选项卡的onPause()方法中(extends fragment)。当我在选项卡之间切换时,它工作正常,但当我移动到下一页(下一个活动)并返回时,它会抛出nullpointer异常。

因为当我切换到下一个活动时,onPause()地图片段已被删除并提交,当它返回时,它发现它为null并抛出异常。我无法弄清楚,在哪里放置我的onPause()方法代码或者如何在重启或恢复时再次启动片段。

公共类LocatorMainActivity扩展了FragmentActivity {

public class MapSectionFragment extends Fragment {
    GoogleMap map;

    public MapSectionFragment() {

    }


    /*
     * (non-Javadoc)
     * 
     * @see android.support.v4.app.Fragment#onDestroyView()
     */
    @Override
    public void onDestroyView() {
        // TODO Auto-generated method stub
        super.onDestroyView();

        Log.e("", "onDestroyView");

        /*try{

            Fragment fragment = (getFragmentManager()
                    .findFragmentById(R.id.map));

            ft = getFragmentManager()
                    .beginTransaction();


            //ft.addToBackStack(null);
            ft.remove(fragment);
            ft.commit();
            //getFragmentManager().executePendingTransactions();
            //ft.commitAllowingStateLoss();

        }catch(Exception e){
            Log.e("", "Exception::" + e.toString());
        }*/
    }

    @Override
    public void onPause(){

        super.onPause();
        Log.e("", "onPause"); 

        try{

            Fragment fragment = (getFragmentManager()
                    .findFragmentById(R.id.map));

            ft = getFragmentManager()
                    .beginTransaction();

            //if(ft.isAddToBackStackAllowed()){
                //ft.hide(fragment);
            if(ft.isAddToBackStackAllowed()){

                Log.e("", "back stack allowed"); 
                ft.remove(fragment);
                //ft.addToBackStack(getTag());
                //ft.addToBackStack(null);
                ft.commit();
            }

            //}

            //getFragmentManager().executePendingTransactions();
            //ft.commitAllowingStateLoss();

        }catch(Exception e){
            Log.e("", "Exception::" + e.toString());
        }
    }


/*  
    @Override
    public void onResume(){

        super.onResume();
        Log.e("inner", "onResume"); 

    }*/


    @Override
    public void onStop() {
        super.onStop();
        Log.e("inner", "onStop"); 

        //mReturningWithResult = false;

    }

    View rootView = null;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        //code
        });
        return rootView;
    }
}

}

请建议我哪里错了。 谢谢。

2 个答案:

答案 0 :(得分:2)

在我的工作地图片段代码中,我已经在onDestroyView()方法中删除/提交了地图片段(我正在使用SupportMapFragment),并且我已经在onActivityCreated中初始化了地图( )方法,如下图所示:

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    FragmentManager fm = getFragmentManager();
    myMapFragment = (SupportMapFragment) fm.findFragmentById(R.id.id_for_your_map_fragment);
    if (myMapFragment == null) {
        myMapFragment = (SupportMapFragment) SupportMapFragment.newInstance();
        /* Code omitted here... all you have to do here is get your
         * fragment manager and run a replace() operation to swap the new fragment in
         */
    }
    myMap = myMapFragment.getMap();
}

@Override
public void onDestroyView () {
    super.onDestroyView();

    try {
        FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
        ft.remove(myMapFragment).commit();
    }
    catch (Exception e) {
        e.printStackTrace();
    }
}

最后,在onResume()中,我运行此检查以确保映射不为null;如果是,我运行getMap()方法:

@Override
public void onResume() {
    if (myMap == null) {
        FragmentManager fm = getFragmentManager();
        myMapFragment = (SupportMapFragment) fm.findFragmentById(R.id.id_for_your_map_fragment);
        myMap = myMapFragment.getMap();
    }
}

将这些片段安排在上面指定的地方到目前为止对我有用。我希望这有帮助! :)

答案 1 :(得分:2)

通过输入以下代码解决了我的问题:

public class Fragment1 extends Fragment {

    GoogleMap map;

    @Override
    public void onStart() {
        // TODO Auto-generated method stub
        map = ((SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map)).getMap();
        super.onStart();

    }

    /*
     * (non-Javadoc)
     * 
     * @see android.support.v4.app.Fragment#onDestroyView()
     */
    @Override
    public void onDestroyView() {
        // TODO Auto-generated method stub
        Fragment fragment = (getFragmentManager()
                .findFragmentById(R.id.map));
        if (fragment.isResumed()) {
            getFragmentManager().beginTransaction().remove(fragment)
                    .commit();
        }
        super.onDestroyView();
    }