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 rec开发者_StackOverflow中文版eived (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.
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:
- Create a new MapActivity to show the map
- Create a TabActivity and add the map as a new tab
- Use a ViewSwitcher to decide whether to show the map or not.
精彩评论