0

I hope I can explain this properly. I'm making an android app that, when you open it, it connects to a JSON server, pulls down the data (GPS coords) and dynamically creates a "menu" based on what it received (View1). This will consist of a few buttons, which when clicked, will load a MapView (View2) with the coords gotten from the JSON represented as markers on the map.

I start off with setContentView(R.layout.menu) then get the data, onbuttonClick I load setContentView(R.layout.map) and draw the markers. The problem is, I have an onLocationChangedListener that goes through the code to set up the menu initially. When it tries to build the menu when the mapView is open, I get a force close. Unfortunately, this code also updates the user location and the locations of the overlays and re-draws the map.

My question is: Can I do a check on a layout to say something like if (isActive) so that I can perform actions only if the current view is in focus?

OR should I scrap the whole thing and start again with a better layout? (Suggestions welcome)

Summary::: I have 2 views (menu,map). Need access to same data across both. Currently works with setContentView() but gives me a Force Close when actions are performed on inactive view.

eoinzy
  • 2,152
  • 4
  • 36
  • 67

1 Answers1

2

If I understand correctly, you are using setContentView(someLayoutId) to change each time what the Activity is displaying. This is not the way android apps usually work. When you retrieve a resource, you need a root element to reference it, that's why you get the exceptions when the View is not "active".

You have several other options to evaluate:

Aleadam
  • 40,203
  • 9
  • 86
  • 108
  • I was following the Hello World Maps guide on developer.android.com which implements MapActivity in the MainActivity. I might look into that ViewSwitcher. I've never heard of it before! – eoinzy Jun 02 '11 at 20:32
  • @eoinzy the problem is to call twice setContentView(). With the second call, you are loosing the reference to the Views inflated in the first one. – Aleadam Jun 02 '11 at 20:46
  • 1
    Ye, I figured that was it. I come from a C++ background and this is something I never thought I'd say back at the start but, I wish I could use pointers, especially to create maybe a Linked List of views, with parents and children. Anyway, I've hacked together a solution->Global BOOL telling me if I'm in the mapview or not (switched just before each setContentView), and only call the functions if I'm not. Horrible code admittedly but what can ya do!! :D – eoinzy Jun 02 '11 at 21:41
  • PS, on my next little project I'm definately gonna look at alternative views, like the TabActivity and ViewSwitcher! – eoinzy Jun 02 '11 at 21:41