我有一个应用程序客户端服务器thing...which只是一个真正的跟踪设备。我的意思是,客户端在安卓手机上,它向服务器发送GPS data....reaching数据到服务器,我想把它发送到另一个活动.
数据在工作线程中接收,并发送到主活动。
为此,我使用了一个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课程:
公共类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类中获取这些点的活动:
公共空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
}问题:
为什么当我试图称之为最后一次活动时,我得到了一个错误:
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 tracker.It,它有一部分是实时tracking.What做的,mean???..............It意味着客户端它是一条路径,所有描述他沿着路径移动的全球定位系统数据都被发送到服务器。
每次更改位置时,都会将新数据发送到服务器部分。
服务器接收工作者thread.....and中的这些点,然后将它们传递给ServerManager类。
现在,当我调用该活动(我最初问题中的最后一段代码)时,我希望它从ServerManager那里询问最后一个数据,他是has.....And,我希望它能够要求它,只要客户机移动.。
U说,我应该使用一个处理程序将数据从woker发送到activity....But,否则我希望.--只要工作人员收到新的更新,活动就会发送给数据。
你现在明白了吗?
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方法.我没有错误,但我想知道应该在其中使用什么循环,以便它能够要求数据,只要我在activity...and中,在我离开活动后就停止要求数据?
要使用So....What循环吗?
最后,谢谢@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;
}
}发布于 2011-05-09 12:15:57
您正在尝试在这里解析null:longitude=Integer.parseInt(ServerManager.getInstance().getLastLongitude());
因为您从getLastLongitude()接收null
好像工作线程还没给setLastLatitude打电话..。
编辑:
doInBackground()中使用循环,在循环的末尾添加sleep(X),其中X是检查之间的间隔。onPause()方法中调用线程上的cancel (并将创建移动到onResume() )。H 212G 213示例:
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.
}
}
...https://stackoverflow.com/questions/5936579
复制相似问题