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 s开发者_JAVA百科everal 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.
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.
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.
精彩评论