在使用包含地图片段的片段时使用onbackpressed()

时间:2014-10-27 08:03:55

标签: java android listview android-fragments

我正在开发一个项目,其中有许多片段连接到片段管理器。我想在片段活动中实现onbackpressed方法,以便我可以回到之前的片段。

onbackpressedcode如下所示:

public void onBackPressed() {

                int count = getSupportFragmentManager().getBackStackEntryCount();

                if (count == 0) {
                    super.onBackPressed();
                    //additional code
                } else {


                    getSupportFragmentManager().popBackStackImmediate();
                }

            }

问题是片段之一中有一个supportmapfragment。该片段中有一个listview。当我点击listview中的一个项目时,它会带我到另一个包含详细信息的片段。当我按下后我得到一个非法状态例外如下所示:

10-27 13:31:26.039: E/AndroidRuntime(15458): FATAL EXCEPTION: main
10-27 13:31:26.039: E/AndroidRuntime(15458): android.view.InflateException: Binary XML file line #8: Error inflating class fragment
10-27 13:31:26.039: E/AndroidRuntime(15458):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:587)
10-27 13:31:26.039: E/AndroidRuntime(15458):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
10-27 13:31:26.039: E/AndroidRuntime(15458):    at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
10-27 13:31:26.039: E/AndroidRuntime(15458):    at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
10-27 13:31:26.039: E/AndroidRuntime(15458):    at com.igloo.fragments.ViewjobsFragment.onCreateView(ViewjobsFragment.java:123)
10-27 13:31:26.039: E/AndroidRuntime(15458):    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
10-27 13:31:26.039: E/AndroidRuntime(15458):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:938)
10-27 13:31:26.039: E/AndroidRuntime(15458):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
10-27 13:31:26.039: E/AndroidRuntime(15458):    at android.support.v4.app.BackStackRecord.popFromBackStack(BackStackRecord.java:764)
10-27 13:31:26.039: E/AndroidRuntime(15458):    at android.support.v4.app.FragmentManagerImpl.popBackStackState(FragmentManager.java:1527)
10-27 13:31:26.039: E/AndroidRuntime(15458):    at android.support.v4.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:494)
10-27 13:31:26.039: E/AndroidRuntime(15458):    at com.igloo.marinaretail.UserMain.onBackPressed(UserMain.java:476)
10-27 13:31:26.039: E/AndroidRuntime(15458):    at android.app.Activity.onKeyUp(Activity.java:1898)
10-27 13:31:26.039: E/AndroidRuntime(15458):    at android.view.KeyEvent.dispatch(KeyEvent.java:1280)
10-27 13:31:26.039: E/AndroidRuntime(15458):    at android.app.Activity.dispatchKeyEvent(Activity.java:2078)
10-27 13:31:26.039: E/AndroidRuntime(15458):    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1705)
10-27 13:31:26.039: E/AndroidRuntime(15458):    at android.view.ViewRoot.deliverKeyEventToViewHierarchy(ViewRoot.java:2626)
10-27 13:31:26.039: E/AndroidRuntime(15458):    at android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2601)
10-27 13:31:26.039: E/AndroidRuntime(15458):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1873)
10-27 13:31:26.039: E/AndroidRuntime(15458):    at android.os.Handler.dispatchMessage(Handler.java:99)
10-27 13:31:26.039: E/AndroidRuntime(15458):    at android.os.Looper.loop(Looper.java:130)
10-27 13:31:26.039: E/AndroidRuntime(15458):    at android.app.ActivityThread.main(ActivityThread.java:3689)
10-27 13:31:26.039: E/AndroidRuntime(15458):    at java.lang.reflect.Method.invokeNative(Native Method)
10-27 13:31:26.039: E/AndroidRuntime(15458):    at java.lang.reflect.Method.invoke(Method.java:507)
10-27 13:31:26.039: E/AndroidRuntime(15458):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
10-27 13:31:26.039: E/AndroidRuntime(15458):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
10-27 13:31:26.039: E/AndroidRuntime(15458):    at dalvik.system.NativeStart.main(Native Method)
10-27 13:31:26.039: E/AndroidRuntime(15458): Caused by: java.lang.IllegalArgumentException: Binary XML file line #8: Duplicate id 0x7f06005e, tag null, or parent id 0x0 with another fragment for com.google.android.gms.maps.SupportMapFragment
10-27 13:31:26.039: E/AndroidRuntime(15458):    at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:296)
10-27 13:31:26.039: E/AndroidRuntime(15458):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
10-27 13:31:26.039: E/AndroidRuntime(15458):    ... 26 more

如何在按下时阻止上述问题。

同样在包含地图的片段的ondestroy方法中,我有以下代码:

@Override
    public void onDestroy() {

        SupportMapFragment f = (SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map);
        if (f.isResumed()){ 
            getActivity().getSupportFragmentManager().beginTransaction().remove(f).commit();
        }

        super.onDestroy();
    }

如何修复背压问题?

有没有办法检查堆栈中的前一个片段是否是包含supportmap的片段?

1 个答案:

答案 0 :(得分:4)

尝试在包含map的片段类中执行以下代码:

public void onDestroyView() 
 {
    super.onDestroyView(); 
    Fragment fragment = (getSupportFragmentManager().findFragmentById(R.id.map));  
    FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
    ft.remove(fragment);
    ft.commit();
}

因为我看到你的评论我认为你只是初始化你的googleMap只是因为它是空的,你有类似下面的代码,

if (googleMap == null)
{

   // initialize Map
}

删除部分并初始化地图。