我的位置应用确定无误。现在我的目的是将应用程序更改为Journey Monitoring。这个想法是从包含当前车辆纬度和经度的网站获取信息。我将url请求添加到了runnable中。我收到 java.lang.IllegalStateException:不在主线程, com.google.android.gms.maps.internal.IGoogleMapDelegate $ a $ a.addMarker(未知来源)和 com.google.android.gms.maps.GoogleMap.addMarker(未知来源)。我该怎么解决这个问题呢。
的活动:
package tri.mylocapp;
//import...
public class MainActivity extends Activity{
GoogleMap googlemap = null;
LatLng latlng;
LocationManager lm;
String provider;
Location location;
MarkerOptions marker;
double lat,lng;
URL url;
URLConnection conn;
String str;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (isGooglePlay()){
setContentView(R.layout.activity_main);
setUpMap();
}
}
private boolean isGooglePlay(){
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (status == ConnectionResult.SUCCESS){
Log.d("mytag", "google ok");
return true;
}
else{
Log.d("mytag","google not ok");
Toast.makeText(this, "GooglePlay is not available", Toast.LENGTH_SHORT).show();
}
return false;
}
private void setUpMap(){
if (googlemap == null){
Log.d("mytag", "googlemap is null, making it available");
googlemap = ((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();
if (googlemap != null){
Log.d("mytag", "googlemap is not null");
googlemap.setMyLocationEnabled(true);
Runnable r = new Runnable() {
@Override
public void run() {
try {
url = new URL("http://thinktodo.net/LocationServiceSystem/index.php/location/getLocation?username=1");
conn = (HttpURLConnection) url.openConnection();
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
str = br.readLine();
JSONObject jsonObject = new JSONObject(str);
final double lat = jsonObject.getDouble("lat");
final double lng = jsonObject.getDouble("lng");
latlng = new LatLng(lat, lng);
marker = new MarkerOptions().position(new LatLng(latlng.latitude, latlng.longitude)).title("You are here");
googlemap.addMarker(marker);
googlemap.moveCamera(CameraUpdateFactory.newLatLng(latlng));
googlemap.animateCamera(CameraUpdateFactory.zoomTo(10));
br.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
}
};
Thread th = new Thread(r);
th.start();
}
}
}
}
main_activity.xml:
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.MapFragment"/>
logcat的:
04-23 02:49:51.250: E/SocketStream(112): readFully was waiting for 403440 bytes, got 49152
04-23 02:49:51.254: E/SocketStream(112): readFully was waiting for 354288 bytes, got 49152
04-23 02:49:51.254: E/SocketStream(112): readFully was waiting for 305136 bytes, got 49152
04-23 02:49:51.254: E/SocketStream(112): readFully was waiting for 255984 bytes, got 70084
04-23 02:49:51.254: E/SocketStream(112): readFully was waiting for 185900 bytes, got 49152
04-23 02:49:51.254: E/SocketStream(112): readFully was waiting for 136748 bytes, got 49152
04-23 02:49:51.254: E/SocketStream(112): readFully was waiting for 87596 bytes, got 49152
04-23 02:49:51.298: E/Trace(1692): error opening trace file: No such file or directory (2)
04-23 02:49:51.858: E/AndroidRuntime(1692): FATAL EXCEPTION: Thread-180
04-23 02:49:51.858: E/AndroidRuntime(1692): java.lang.IllegalStateException: Not on the main thread
04-23 02:49:51.858: E/AndroidRuntime(1692): at maps.k.o.b(Unknown Source)
04-23 02:49:51.858: E/AndroidRuntime(1692): at maps.i.g.b(Unknown Source)
04-23 02:49:51.858: E/AndroidRuntime(1692): at maps.e.al.a(Unknown Source)
04-23 02:49:51.858: E/AndroidRuntime(1692): at etl.onTransact(SourceFile:167)
04-23 02:49:51.858: E/AndroidRuntime(1692): at android.os.Binder.transact(Binder.java:326)
04-23 02:49:51.858: E/AndroidRuntime(1692): at com.google.android.gms.maps.internal.IGoogleMapDelegate$a$a.addMarker(Unknown Source)
04-23 02:49:51.858: E/AndroidRuntime(1692): at com.google.android.gms.maps.GoogleMap.addMarker(Unknown Source)
04-23 02:49:51.858: E/AndroidRuntime(1692): at tri.mylocapp.MainActivity$1.run(MainActivity.java:88)
04-23 02:49:51.858: E/AndroidRuntime(1692): at java.lang.Thread.run(Thread.java:856)
04-23 02:49:51.930: E/SocketStream(112): readFully was waiting for 403440 bytes, got 49152
04-23 02:49:51.930: E/SocketStream(112): readFully was waiting for 354288 bytes, got 49152
04-23 02:49:51.930: E/SocketStream(112): readFully was waiting for 305136 bytes, got 49152
04-23 02:49:51.930: E/SocketStream(112): readFully was waiting for 255984 bytes, got 49152
04-23 02:49:51.930: E/SocketStream(112): readFully was waiting for 206832 bytes, got 16384
04-23 02:49:51.930: E/SocketStream(112): readFully was waiting for 190448 bytes, got 97988
04-23 02:49:51.934: E/SocketStream(112): readFully was waiting for 92460 bytes, got 49152
04-23 02:49:51.934: E/SocketStream(112): readFully was waiting for 43308 bytes, got 32768
答案 0 :(得分:0)
问题是你在不同的线程上修改地图应该在UI线程/主线程上修改..
latlng = new LatLng(lat, lng);
marker = new MarkerOptions().position(new LatLng(latlng.latitude, latlng.longitude)).title("You are here");
googlemap.addMarker(marker);
googlemap.moveCamera(CameraUpdateFactory.newLatLng(latlng));
googlemap.animateCamera(CameraUpdateFactory.zoomTo(10));
这就是问题的来源......
<强>解决方案:强>
使用asynctask执行所有服务器连接并解析 doinbackground ,何时成功修改 postexecute 上的地图