Android – crashes when adding tags to Google Maps
•
Android
I have this method:
class getuser extends AsyncTask<String, String, String> {
protected String doInBackground(String... args) {
int success;
try {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("ID", ID));
JSONObject json = jsonParser.makeHttpRequest(url_user_detials, "GET", params);
Log.d("Single user Details", json.toString());
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
JSONArray userObj = json.getJSONArray("user");
JSONObject user = userObj.getJSONObject(0);
String ln = user.getString("longg");
String lt = user.getString("latt");
String fn = user.getString("fname");
String lna = user.getString("lname");
String ph = user.getString("phone");
username=fn+" "+lna;
userphone=ph+"";
userlat= Double.parseDouble(lt);
userlong= Double.parseDouble(ln);
marker = new MarkerOptions().position(new LatLng(userlat, userlong));
marker.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker));
map.addMarker(marker);
} else {
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
It gets some values from the database and puts them in Google Maps tags. I have an error in this section:
marker = new MarkerOptions().position(new LatLng(userlat, userlong));
marker.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker));
map.addMarker(marker);
Didn't I find any problems?
My mistake is:
04-25 20:22:55.330 24534-29164/com.example.hatim.maps D/Single user Details: {"success":1,"user":[{"ID":"1223456789","fname":"lama","lname":"tat","phone":"2587598","longg":"46.739004409794056","latt":"24.729458546415405"}]}
04-25 20:22:55.350 24534-29164/com.example.hatim.maps E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
04-25 20:22:55.350 24534-29164/com.example.hatim.maps E/AndroidRuntime: Process: com.example.hatim.maps, PID: 24534
04-25 20:22:55.350 24534-29164/com.example.hatim.maps E/AndroidRuntime: java.lang.RuntimeException: An error occurred while executing doInBackground()
04-25 20:22:55.350 24534-29164/com.example.hatim.maps E/AndroidRuntime: at android.os.AsyncTask$3.done(AsyncTask.java:309)
04-25 20:22:55.350 24534-29164/com.example.hatim.maps E/AndroidRuntime: at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
04-25 20:22:55.350 24534-29164/com.example.hatim.maps E/AndroidRuntime: at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
04-25 20:22:55.350 24534-29164/com.example.hatim.maps E/AndroidRuntime: at java.util.concurrent.FutureTask.run(FutureTask.java:242)
04-25 20:22:55.350 24534-29164/com.example.hatim.maps E/AndroidRuntime: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
04-25 20:22:55.350 24534-29164/com.example.hatim.maps E/AndroidRuntime: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
04-25 20:22:55.350 24534-29164/com.example.hatim.maps E/AndroidRuntime: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
04-25 20:22:55.350 24534-29164/com.example.hatim.maps E/AndroidRuntime: at java.lang.Thread.run(Thread.java:818)
04-25 20:22:55.350 24534-29164/com.example.hatim.maps E/AndroidRuntime: Caused by: java.lang.IllegalStateException: Not on the main thread
04-25 20:22:55.350 24534-29164/com.example.hatim.maps E/AndroidRuntime: at maps.ce.i.b(UnkNown Source)
04-25 20:22:55.350 24534-29164/com.example.hatim.maps E/AndroidRuntime: at maps.db.c.a(UnkNown Source)
04-25 20:22:55.350 24534-29164/com.example.hatim.maps E/AndroidRuntime: at maps.ei.bu.a(UnkNown Source)
04-25 20:22:55.350 24534-29164/com.example.hatim.maps E/AndroidRuntime: at com.google.android.gms.maps.internal.e$a.onTransact(:com.google.android.gms.alldynamite:167)
04-25 20:22:55.350 24534-29164/com.example.hatim.maps E/AndroidRuntime: at android.os.Binder.transact(Binder.java:387)
04-25 20:22:55.350 24534-29164/com.example.hatim.maps E/AndroidRuntime: at com.google.android.gms.maps.internal.IGoogleMapDelegate$zza$zza.addMarker(UnkNown Source)
04-25 20:22:55.350 24534-29164/com.example.hatim.maps E/AndroidRuntime: at com.google.android.gms.maps.GoogleMap.addMarker(UnkNown Source)
04-25 20:22:55.350 24534-29164/com.example.hatim.maps E/AndroidRuntime: at com.example.hatim.maps.SearchResutl$getuser.doInBackground(SearchResutl.java:181)
04-25 20:22:55.350 24534-29164/com.example.hatim.maps E/AndroidRuntime: at com.example.hatim.maps.SearchResutl$getuser.doInBackground(SearchResutl.java:156)
04-25 20:22:55.350 24534-29164/com.example.hatim.maps E/AndroidRuntime: at android.os.AsyncTask$2.call(AsyncTask.java:295)
04-25 20:22:55.350 24534-29164/com.example.hatim.maps E/AndroidRuntime: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
04-25 20:22:55.350 24534-29164/com.example.hatim.maps E/AndroidRuntime: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
04-25 20:22:55.350 24534-29164/com.example.hatim.maps E/AndroidRuntime: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
04-25 20:22:55.350 24534-29164/com.example.hatim.maps E/AndroidRuntime: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
04-25 20:22:55.350 24534-29164/com.example.hatim.maps E/AndroidRuntime: at java.lang.Thread.run(Thread.java:818)
Can someone help me solve this problem? Thank you in advance
resolvent:
You cannot update the UI from the background thread. To update the UI or add tags to the map, use the onpostexecute method. You can also use the
runOnUiThread(new Runnable() {
@Override
public void run() {
// your UI updation
}
});
You are using asynctask, and then update the UI in the onpostexecute method. Update your asynctask as follows
class getuser extends AsyncTask<String, String, LatLng> {
@Override
protected LatLng doInBackground(String... strings) {
int success;
try {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("ID", ID));
JSONObject json = jsonParser.makeHttpRequest(url_user_detials, "GET", params);
Log.d("Single user Details", json.toString());
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
JSONArray userObj = json.getJSONArray("user");
JSONObject user = userObj.getJSONObject(0);
String ln = user.getString("longg");
String lt = user.getString("latt");
String fn = user.getString("fname");
String lna = user.getString("lname");
String ph = user.getString("phone");
username=fn+" "+lna;
userphone=ph+"";
userlat= Double.parseDouble(lt);
userlong= Double.parseDouble(ln);
return new LatLng(userlat, userlong);
} else {
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(LatLng latLng) {
if(latLng != null){
marker = new MarkerOptions().position(latLng);
marker.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker));
map.addMarker(marker);
}
}
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码