0

I am trying to open and close maps (changing layouts) with a button.

I have a button in MainActivity which opens SecondActivity and this sets maps_layout correct.

I have a button on the maps. This switches to another layout correctly.

When I want to switch back, I get Fatal Exception.

My Code:

public class Game extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mapy();
    }

    public void mapy() {
        setContentView(R.layout.mapy_test);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map_test);
        mapFragment.getMapAsync(this);

        Button changetocam = (Button) findViewById(R.id.changetocam);

        changetocam.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cam();
            }
        });
    }

    public void cam() {
        setContentView(R.layout.ingame_masteroverlay_layout);

        Button changetomap = (Button) findViewById(R.id.changetomap);

        changetomap.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mapy();
            }
        });
    }
}

Here is the error log:

`FATAL EXCEPTION: main Process: auftour.mrorhan.probieren, PID: 2689 android.view.InflateException: Binary XML file line #6: Binary XML file line #6: Error inflating class fragment Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class fragment Caused by: java.lang.IllegalArgumentException: Binary XML file line #6: Duplicate id 0x7f0f010b, tag null, or parent id 0xffffffff with another fragment for com.google.android.gms.maps.SupportMapFragment` 

I think the error is because I don't close the mapy_test layout, so I have double id, when I try to connect to the map again.

CocoNess
  • 4,213
  • 4
  • 26
  • 43
MrOrhan
  • 877
  • 6
  • 21
  • 1
    Could you post your logcat error? – antonio Dec 17 '16 at 22:56
  • FATAL EXCEPTION: main Process: auftour.mrorhan.probieren, PID: 2689 android.view.InflateException: Binary XML file line #6: Binary XML file line #6: Error inflating class fragment Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class fragment Caused by: java.lang.IllegalArgumentException: Binary XML file line #6: Duplicate id 0x7f0f010b, tag null, or parent id 0xffffffff with another fragment for com.google.android.gms.maps.SupportMapFragment – MrOrhan Dec 18 '16 at 14:31
  • Btw, I think the error comes, couse i dont close the "mapy_test" layout, so i have doble id, when i try to connect to maps again ? – MrOrhan Dec 18 '16 at 18:53
  • I edited the question to move the error log out of the comment and into the main question. – CocoNess Dec 20 '16 at 13:03
  • ty for editing :) – MrOrhan Dec 24 '16 at 18:31

2 Answers2

0

// Try this it is working for me View v;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    if (v != null) {
        ViewGroup parent = (ViewGroup) view.getParent();
        if (parent != null)
            parent.removeView(v);
    }
    try
 {
        v = inflater.inflate(R.layout.mapy_test, container, false);
    } catch (InflateException e) {
        /* map is already there, just return view as it is */
    }
    return v;
}
gautam
  • 522
  • 3
  • 14
  • ty for ur answer, should i put it on the place, where I change from map to the other layout ? – MrOrhan Dec 24 '16 at 18:03
  • Are u working with Fragments? i dont work with fragments – MrOrhan Dec 24 '16 at 18:10
  • ok i found the same answer too :) but thank you :p http://stackoverflow.com/questions/14083950/duplicate-id-tag-null-or-parent-id-with-another-fragment-for-com-google-androi – MrOrhan Dec 26 '16 at 15:29
  • I dont used it. I used just fragments and fragmentcontainer. I said just ty for ur effort. – MrOrhan Dec 28 '16 at 15:24
0

So guys, i solve my Problem. I worked with frames and with an extra frame containerlayout. If someone have the same Problem I can write my own code.

MrOrhan
  • 877
  • 6
  • 21