Issue with a ServerManager class
I have an app client-server thing...which is nothing else but a real tracking device. I mean the client is on an android phone and it sends to the server the GPS data....reaching data to the server I wanna send it to another activity....
The data is received in a worker thread and it send to a main activity.
For that I've used a ServerManager class in which the worker thread stores the last GPS data received.
this is the worker thread:
Scanner is = new Scanner(new DataInputStream(
this.clientSocket.getInputStream()));
while (!stop) {
while (is.hasNext()) {
try {
longitude = is.next();
latitude = is.next();
// save last data
ServerManager.getInstance().setLastLatitude(latitude);
ServerManager.getInstance().setLastLongitude(longitude);
} catch (Exception e) {
e.printStackTrace();
}
}
}
This is my ServerManager class:
public class ServerManager {
String lastLatitude;
String lastLongitude;
// singleton
private static ServerManager instance = null;
private ServerManager() {
}
public static ServerManager getInstance() {
if (instance == null)
instance = new ServerManager();
return instance;
}
public String getLastLatitude() {
return lastLatitude;
}
public void setLastLatitude(String lastLatitude) {
this.lastLatitude = lastLatitude;
}
public String getLastLongitude() {
return lastLongitude;
}
public void setLastLongitude(String lastLongitude) {
this.lastLongitude = lastLongitude;
}
}
And finally this is the activity that asks for those points from the ServerManager class:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.server4);
mapView = (MapView) findViewById(R.id.mapview1);
mapView.setBuiltInZoomControls(true);
mc = mapView.getController();
longitude=Integer.parseInt(ServerManager.getInstance().getLastLongitude());
latitude=Integer.parseInt(ServerManager.getInstance().getLastLatitude());
p=new GeoPoint(longitude,latitude);
theRouteDraw(p);//here I draw those points on the map
}
Question:
Why when I try to call this last activity I get this eror:
05-08 23:45:40.409: ERROR/AndroidRuntime(360): Uncaught handler: thread main exiting due to uncaught exception
05-08 23:45:40.459: ERROR/AndroidRuntime(360): java.lang.RuntimeException: Unable t开发者_Go百科o start activity ComponentInfo{com.server/com.server.Server4}: java.lang.NumberFormatException: unable to parse 'null' as integer
05-08 23:45:40.459: ERROR/AndroidRuntime(360): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
05-08 23:45:40.459: ERROR/AndroidRuntime(360): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
05-08 23:45:40.459: ERROR/AndroidRuntime(360): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
05-08 23:45:40.459: ERROR/AndroidRuntime(360): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
05-08 23:45:40.459: ERROR/AndroidRuntime(360): at android.os.Handler.dispatchMessage(Handler.java:99)
05-08 23:45:40.459: ERROR/AndroidRuntime(360): at android.os.Looper.loop(Looper.java:123)
05-08 23:45:40.459: ERROR/AndroidRuntime(360): at
05-08 23:45:40.459: ERROR/AndroidRuntime(360): at java.lang.reflect.Method.invokeNative(Native Method)
05-08 23:45:40.459: ERROR/AndroidRuntime(360): at java.lang.reflect.Method.invoke(Method.java:521)
05-08 23:45:40.459: ERROR/AndroidRuntime(360): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
05-08 23:45:40.459: ERROR/AndroidRuntime(360): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
05-08 23:45:40.459: ERROR/AndroidRuntime(360): at dalvik.system.NativeStart.main(Native Method)
05-08 23:45:40.459: ERROR/AndroidRuntime(360): Caused by: java.lang.NumberFormatException: unable to parse 'null' as integer
05-08 23:45:40.459: ERROR/AndroidRuntime(360): at java.lang.Integer.parseInt(Integer.java:347)
05-08 23:45:40.459: ERROR/AndroidRuntime(360): at java.lang.Integer.parseInt(Integer.java:323)
05-08 23:45:40.459: ERROR/AndroidRuntime(360): at com.server.Server4.onCreate(Server4.java:47)
05-08 23:45:40.459: ERROR/AndroidRuntime(360): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-08 23:45:40.459: ERROR/AndroidRuntime(360): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
UPDATE:
My app is kind of a GPS tracker.It has a part that does real time tracking.What does that mean???..............It means that the client it moves...over a route and all this GPS data that describes his movement along a route is sent to a server.
Each time the location is changed the new data is sent to the server part.
The server receives those points in a worker thread.....and after that I pass them to a ServerManager class.
Now,when I call for that activity(the last code in my initial question) I want it to ask from ServerManager the last data he has.....And I want it to ask for it as long as the client moves....
U said that I should use a handler to sent the data from woker to activity....But I want otherwise....-the activity to as for the data as long as the worker receives new updates.
Do u understand now????
UPDATE2:
protected GeoPoint doInBackground(Void...voids){
try{
longitude=Integer.parseInt(ServerManager.getInstance().getLastLongitude());
latitude=Integer.parseInt(ServerManager.getInstance().getLastLatitude());
p=new GeoPoint(longitude,latitude);
publishProgress(p);
}
catch(Exception e)
{
e.printStackTrace();
}
return p;
}
This is my doInBackground method....I have no errors,but I wonder what loop should I use inside of it in order for it to ask for data as long as I'm in that activity...and stop asking for data as soon as I leave the activity????
So....What loop to use?
Finally,this is what came up thanks@MByD
public class Server4 extends MapActivity {
private LocationManager lm;
private LocationListener locationListener;
private MyLocationOverlay myLocOverlay;
private MapView mapView;
private MapController mc;
GeoPoint p;
InitTask init_task;
int latitude;
int longitude;
DBAdapter db;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.server4);
mapView = (MapView) findViewById(R.id.mapview1);
mapView.setBuiltInZoomControls(true);
mc = mapView.getController();
}
public void onResume() {
super.onResume();
init_task = new InitTask();
init_task.execute();
}
public class InitTask extends AsyncTask<Void, GeoPoint, Void> {
GeoPoint p;
protected Void doInBackground(Void... voids) {
try {
while (true) {
longitude = Integer.parseInt(ServerManager.getInstance()
.getLastLongitude());
latitude = Integer.parseInt(ServerManager.getInstance()
.getLastLatitude());
p = new GeoPoint(longitude, latitude);
publishProgress(p);
Thread.sleep(500);
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
protected void onProgressUpdate(GeoPoint... progress1) {
theRouteDraw(progress1[0]);
//initMyLocation();
}
}
public void onPause(){
init_task.cancel(true);
super.onPause();
}
public void theRouteDraw(GeoPoint p1) {
mc.animateTo(p1);
mc.setZoom(13);
mapView.invalidate();
mapView.setSatellite(true);
}
/*private void initMyLocation() {
myLocOverlay = new MyLocationOverlay(this, mapView);
myLocOverlay.enableMyLocation();
mapView.getOverlays().add(myLocOverlay);
}*/
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
You are trying to parse null here: longitude=Integer.parseInt(ServerManager.getInstance().getLastLongitude());
Because you receive null from getLastLongitude()
Seems like worker thread didn't call setLastLatitude
yet...
Edit:
Use loop inside
doInBackground()
and addsleep(X)
at the end of the loop where X is the interval between checks.Call
cancel
on the thread atonPause()
method (and move the creation toonResume()
as well).
Example:
try{
while(keepGoing) // some boolean that is set to true
{
longitude=Integer.parseInt(ServerManager.getInstance().getLastLongitude());
latitude=Integer.parseInt(ServerManager.getInstance().getLastLatitude());
p=new GeoPoint(longitude,latitude);
publishProgress(p);
Thread.sleep(interval); // interval specifies the time to wait in ms.
}
}
...
精彩评论