将数据放入内部类时遇到问题
我正在尝试编写一个稍微超出我能力的程序。该程序应该检索 GPS 数据并将其显示在漂亮的图形页面上。通过实现必要的侦听器接口,我可以很好地访问 GPS 数据。我可以使用 SurfaceView 的复杂程序结构来显示漂亮的图形。但是,当我尝试将这两个功能(传入数据和图形)整合在一起时,事情就分崩离析了。
该程序的图形结构以示意图形式表示:
Activity 实现了几个 gpsStatus/Location 接口
Anonymous inner class Panel extends SurfaceView implements SurfaceHolder.Callback
onDraw() which does the drawing
inner (instance) class Thread
run() which does the canvas locking and calls onDraw()
我希望这是有意义的。我从 Droidnova 网站 2D 图形教程中获得了这个结构。图形是在单独的线程中绘制的。我已经能够从中获得非常漂亮、平滑的图形。
扩展 SurfaceView 的“Panel”类是在 Activity 的 onCreate 中创建的,如下所示: setContentView(new Panel(this));内部类由Panel 的构造函数创建为实例,即Thread thread=new Thread()。
我的 GPS 芯片数据是通过活动中的方法传入的。问题是,如何将这些对象放入“Panel”并放入 onDraw() 中?在 onDraw() 中,无法访问 Activity 中的对象(我的 GPS 数据)。
我尝试创建 SurfaceView 作为实例,我尝试将 SurfaceView 移出,使其成为一个单独的类(线程仍在内部)。我尝试过在面板中实现接口这些接口总是崩溃,调试数据我无法理解。
非常感谢任何帮助。
I'm trying to write a program that is just a little beyond my abilities.The program is supposed to retrieve GPS data and display it on a nice page of graphics. I can access the GPS data fine, by implementing the necessary listener interfaces. I can display nice graphics using a sophisticated program structure employing a SurfaceView. But when I try to pull these two functionalites (incoming data and graphics) together things fall apart.
The graphics structure of the program is, in schematic form:
Activity implements several gpsStatus/Location interfaces
Anonymous inner class Panel extends SurfaceView implements SurfaceHolder.Callback
onDraw() which does the drawing
inner (instance) class Thread
run() which does the canvas locking and calls onDraw()
I hope that makes sense. I got this structure from the Droidnova site, the 2D graphics tutorial. The graphics are drawn in the separate thread. I've been able to get very nice smooth graphics out of this.
The class "Panel," the one which extends SurfaceView is created in the onCreate of the Activity, like this: setContentView(new Panel(this)); The inner class is created as an instance, i.e. Thread thread=new Thread() by the Panel's constructor.
My data from the GPS chip comes in via methods in the Activity. The problem is, how do I get these objects inside the "Panel" and into the onDraw()? From within the onDraw() the objects in the Activity (my GPS data) are inaccessible.
I've tried creating the SurfaceView as an instance, I've tried moving the SurfaceView out so it's a separate class (with the thread still inside). I've tried implementing the interfaces in the Panel These always crash, with debug data I can't fathom.
Any help much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为,如果您将 GPS 数据包装到类 foo 中,并将对 foo 实例的引用传递到 Panel 上的构造函数中,您应该拥有所需的一切。
I would think that if you wrapped your GPS data into a class foo, and pass a reference to an instance of foo into a constructor on Panel, you should have all you need.
如果内部类是在Activity类内部声明的(监听gps数据),那么内部类应该能够访问Activity类的成员变量。
它不是很面向对象,但应该可以工作。
If the inner class is declared inside the Activity class (listening to the gps data), then the inner class should be able to access the member variables of the Activity class.
It is not very OO, but it should work.