gpt4 book ai didi

android - 在谷歌地图上添加标记时崩溃

转载 作者:太空宇宙 更新时间:2023-11-03 13:49:34 26 4
gpt4 key购买 nike

我有这个方法:

       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;
}

从数据库中获取一些值并将其放在谷歌地图标记上。我在这部分有错误:

     marker = new MarkerOptions().position(new LatLng(userlat, userlong));
marker.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker));
map.addMarker(marker);

我看不出有什么问题吗?

我得到的错误是:

      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) 

有人可以帮我解决这个问题吗?提前致谢。

最佳答案

您不能从后台线程更新 UI。要更新 UI 或将标记添加到 map ,请使用 onPostExecute 方法。你也可以使用

runOnUiThread(new Runnable() {
@Override
public void run() {
// your UI updation
}
});

您正在使用 AsyncTask,然后在 onPostExecute 方法中更新 UI。按如下方式更新您的 AsyncTask

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);
}


}
}

关于android - 在谷歌地图上添加标记时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36847194/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com