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

时间:2016-04-25 17:27:07

标签: android google-maps android-studio

我有这个方法:

     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) 

我没有看到任何问题?

我得到的错误是:

<log4net xmlns="urn:log4net">
  <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
    <threshold value="All" />
    <file value="../Logs/MyApp/myapp.log" />
    <appendToFile value="true" />
    <rollingStyle value="Date" />
    <datePattern value="yyyy-MM-dd.'log'" />
    <maxSizeRollBackups value="5" />
    <maximumFileSize value="10MB" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%d{HH':'mm':'ss'.'fff} [%4t] %-5p %M - %m %15property{ipAddress} - %property{orderId}%n" />
    </layout>
  </appender>
  <appender name="SmtpAppender" type="log4net.Appender.SmtpAppender">
    <to value="<your to address>" />
    <from value="<your from address>" />
    <subject value="<your subject line>" />
    <smtpHost value="<your smtp host>"/>
    <username value="<your username>" />
    <password value="<your password>" />
    <port value="25" />
    <authentication value="Basic" />
    <evaluator type="log4net.Core.LevelEvaluator">
      <threshold value="Error" />
    </evaluator>
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%newline[%property{log4net:HostName}] [%date] [%-5level] [%logger] - [%message]%newlineIp[%property{IpAddress}] Session[%property{SessionId}] OrderId[%property{OrderId}] UserAgent[%property{UserAgent}]%newline%newline%exception" />
    </layout>
  </appender>

  <root>
    <level value="ALL" />
    <appender-ref ref="RollingLogFileAppender" />
    <appender-ref ref="SmtpAppender" />
  </root>
</log4net>
有人可以帮我解决这个问题吗?提前谢谢。

1 个答案:

答案 0 :(得分:3)

您无法从后台线程更新UI。要更新UI或添加标记以映射使用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);
           }


        }
    }