1

在我的应用程序中有一个包含三个选项卡(地图、列表、设置)的片段活动。我已将删除和提交放在地图选项卡的 onPause() 方法(扩展片段)。当我在选项卡之间切换时,它工作正常,但是当我移动到下一页(下一个活动)并返回时,它会引发空指针异常。

因为当我切换到下一个活动时,onPause() 地图片段已被删除并提交,当它返回时发现它为空并引发异常。我无法弄清楚在哪里放置我的 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;
    }
}

}

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

4

2 回答 2

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();
    }
于 2013-11-12T07:22:56.780 回答
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();
    }
}

And finally, within onResume(), I run this check to make sure that the map is not null; if it is, I run the getMap() method:

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

Having these snippets arranged in the places specified above has worked for me so far. I hope this helps! :)

于 2013-11-11T07:37:58.447 回答