帮助进行代码布局
我希望我能正确解释这一点。 我正在制作一个 Android 应用程序,当您打开它时,它会连接到 JSON 服务器,提取数据(GPS 坐标)并根据收到的内容(View1)动态创建一个“菜单”。这将由几个按钮组成,单击这些按钮时,将加载 MapView (View2),其中从 JSON 获取的坐标表示为地图上的标记。
我从 setContentView(R.layout.menu)
开始,然后获取数据,在 onbuttonClick 上加载 setContentView(R.layout.map)
并绘制标记。问题是,我有一个 onLocationChangedListener
,它通过代码来最初设置菜单。当它在 mapView
打开时尝试构建菜单时,我会强制关闭。不幸的是,此代码还会更新用户位置和叠加层的位置并重新绘制地图。
我的问题是:我是否可以对布局进行检查以说出类似 if (isActive)
的内容,以便仅在当前视图处于焦点状态时才能执行操作?
或者我应该放弃整个事情并以更好的布局重新开始? (欢迎提出建议)
摘要::: 我有 2 个观点(菜单、地图)。需要在两者之间访问相同的数据。目前与 setContentView()
配合使用,但在非活动视图上执行操作时会强制关闭。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我理解正确,您将使用 setContentView(someLayoutId) 来更改每次 Activity 显示的内容。这不是 Android 应用程序通常的工作方式。当您检索资源时,您需要一个根元素来引用它,这就是为什么当视图不“活动”时您会收到异常。
您还有其他几个选项可供评估:
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: