ServerManager 类的问题
我有一个应用程序客户端服务器的东西......这只是一个真正的跟踪设备。 我的意思是客户端位于 Android 手机上,它将 GPS 数据发送到服务器......将数据发送到服务器,我想将其发送到另一个活动......
数据在工作线程中接收并发送到一项主要活动。
为此,我使用了 ServerManager 类,其中工作线程存储最后接收到的 GPS 数据。
这是工作线程:
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();
}
}
}
这是我的 ServerManager 类:
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;
}
}
最后,这是从 ServerManager 类请求这些点的活动:
public void onCreate(Bundle savingInstanceState) {
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
}
问题:
为什么当我尝试调用它时上次活动时我收到此错误:
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 to 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)
更新:
我的应用程序是一种 GPS 跟踪器。它有一个部分可以进行实时跟踪。这是什么意思???............它意味着客户端在一条路线上移动,所有这些 GPS 数据描述他沿着路线的移动被发送到服务器。
每次位置更改时,新数据都会发送到服务器部分。
服务器在工作线程中接收这些点......然后我将它们传递给 ServerManager 类。
现在,当我调用该活动(我最初问题中的最后一个代码)时,我希望它向 ServerManager 询问他拥有的最后数据......并且只要客户端移动,我希望它询问它...... ..
你说我应该使用处理程序将数据从woker发送到活动....但我想要另外的....-只要工作人员收到新的更新,活动就可以获取数据。
现在你明白了吗????
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;
}
这是我的 doInBackground 方法...我没有错误,但我想知道我应该在其中使用什么循环,以便它在我处于该活动时请求数据...并停止询问我一离开活动就获取数据???
那么......使用什么循环?
最后,这就是结果,谢谢@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;
}
}
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 to 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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您试图在这里解析 null:
longitude=Integer.parseInt(ServerManager.getInstance().getLastLongitude());
因为您从
getLastLongitude()
收到 null看起来像工作者线程尚未调用
setLastLatitude
...编辑:
在
doInBackground()
内使用循环并在处添加sleep(X)
循环末尾,其中 X 是检查之间的间隔。在线程的
onPause()
方法上调用cancel
(并将创建也移至onResume()
)。例子:
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: