IllegalArgumentException:找不到id的视图,从另一个片段和onResume调用返回时

时间:2015-03-19 15:17:45

标签: android google-maps android-fragments

我有一个片段,其大部分布局都是以编程方式创建

第一次出现片段一切正常但是当我打开另一个片段然后按后退按钮时,我收到了IllegalArgumentException。

请注意,当我按下后退按钮时会发生此问题,当我拨打 popBackStack 时,一切正常。

有什么建议吗?

代码:

if(mapView != null)
{
    ViewGroup parent = (ViewGroup) mapView.getParent();
    if (parent != null)
    {
        parent.removeView(mapView);
    }
}
try
{
    // Creating view
    mapView = getBaseActivity().getLayoutInflater().inflate(R.layout.property_show_map_section, (ViewGroup) view, false);
}
catch(Exception e)
{

}
((LinearLayout)view.findViewById(R.id.section_container)).addView(mapView);
final SupportMapFragment googleMap = new SupportMapFragment();
getChildFragmentManager().beginTransaction()
    .replace(R.id.map_container, googleMap).commit();

LogCat:

03-19 18:28:40.229: E/AndroidRuntime(6850): FATAL EXCEPTION: main
03-19 18:28:40.229: E/AndroidRuntime(6850): java.lang.IllegalArgumentException: No view found for id 0x7f0b00e4 (com.example.app:id/map_container) for fragment SupportMapFragment{53056b1c #0 id=0x7f0b00e4}
03-19 18:28:40.229: E/AndroidRuntime(6850):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:939)
03-19 18:28:40.229: E/AndroidRuntime(6850):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
03-19 18:28:40.229: E/AndroidRuntime(6850):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1108)
03-19 18:28:40.229: E/AndroidRuntime(6850):     at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1917)
03-19 18:28:40.229: E/AndroidRuntime(6850):     at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1800)
03-19 18:28:40.229: E/AndroidRuntime(6850):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:967)
03-19 18:28:40.229: E/AndroidRuntime(6850):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
03-19 18:28:40.229: E/AndroidRuntime(6850):     at android.support.v4.app.BackStackRecord.popFromBackStack(BackStackRecord.java:957)
03-19 18:28:40.229: E/AndroidRuntime(6850):     at android.support.v4.app.FragmentManagerImpl.popBackStackState(FragmentManager.java:1541)
03-19 18:28:40.229: E/AndroidRuntime(6850):     at android.support.v4.app.FragmentManagerImpl$2.run(FragmentManager.java:493)
03-19 18:28:40.229: E/AndroidRuntime(6850):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
03-19 18:28:40.229: E/AndroidRuntime(6850):     at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:454)
03-19 18:28:40.229: E/AndroidRuntime(6850):     at android.os.Handler.handleCallback(Handler.java:730)
03-19 18:28:40.229: E/AndroidRuntime(6850):     at android.os.Handler.dispatchMessage(Handler.java:92)
03-19 18:28:40.229: E/AndroidRuntime(6850):     at android.os.Looper.loop(Looper.java:137)
03-19 18:28:40.229: E/AndroidRuntime(6850):     at android.app.ActivityThread.main(ActivityThread.java:5103)
03-19 18:28:40.229: E/AndroidRuntime(6850):     at java.lang.reflect.Method.invokeNative(Native Method)
03-19 18:28:40.229: E/AndroidRuntime(6850):     at java.lang.reflect.Method.invoke(Method.java:525)
03-19 18:28:40.229: E/AndroidRuntime(6850):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
03-19 18:28:40.229: E/AndroidRuntime(6850):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-19 18:28:40.229: E/AndroidRuntime(6850):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:3)

您可能希望覆盖第二个活动中的onBackPressed()方法。

如果你在片段中,你可以这样做:

public boolean onKeyDown(int keyCode, KeyEvent event) {

        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:

                return true
        }
        return false;
}

或者,如果您发布的代码位于活动中,您可以尝试将getChildFragmentManager()更改为getFragmentManager()getSupportFragmentManager()

getChildFragmentManager()更适用于Fragment,getFragmentManager()更多适用于Activity。您可以查看this answer了解详情。