空对象引用上的'com.google.android.gms.maps.model.LatLng com.google.android.gms.maps.model.MarkerOptions.getPosition()'

时间:2019-12-21 05:01:00

标签: android nullpointerexception marker

我想在android studio中的两个标记之间确定方向。我从mysql数据库中使用json获取的第一个标记,第二个从用户当前位置获取的标记。

我使用json从mysql获取纬度和经度。这是json代码

public void showList(String myid) {
        String myURL = "https://ruthambulanceonline.000webhostapp.com/Ambulance_Online/instansi2.php?idku="+myid;
        StringRequest stringRequest = new StringRequest(Request.Method.GET, myURL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    Log.e("Response: ", response.toString());
                    try {
                        JSONObject jObj = new JSONObject(response);
                        String getObject = jObj.getString("instansi");
                        JSONArray jsonArray = new JSONArray(getObject);
                        for (int i = 0; i < jsonArray.length(); i++) {
                            JSONObject jsonObject = jsonArray.getJSONObject(i);
                            title = jsonObject.getString(TITLE);
                            latLngAmbulans = new LatLng(Double.parseDouble(jsonObject.getString(LAT)), Double.parseDouble(jsonObject.getString(LNG)));
                            alamat = jsonObject.getString(ALAMAT);

                            Instansi p = new Instansi(jsonObject.getString("nama_instansi"),jsonObject.getString("alamat_instansi"),jsonObject.getString("notelp_instansi"), jsonObject.getString("id_instansi"));
                            instansiList.add(p);

                            addMarker(latLngAmbulans, title, alamat);
                        }
                        InstansiAdapter adapter = new InstansiAdapter(instansiList,getApplicationContext());
                        listView.setAdapter(adapter);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
        }
    }){
    };
    Handler.getInstance(getApplicationContext()).addRequestQue(stringRequest);
}

这是我的第一个标记的代码

public void addMarker(LatLng latlngAmbulans, final String title, final String alamat) {
    markerOptionsAmbulans = new MarkerOptions().position(latlngAmbulans).title(title).snippet(alamat);
    gMap.addMarker(markerOptionsAmbulans);

}

这是我当前的位置代码

public void onMapReady(GoogleMap googleMap) {
    gMap = googleMap;

    final LatLng latLngPengguna = new LatLng((currentLocation.getLatitude()),currentLocation.getLongitude());
    markerOptionsPengguna = new MarkerOptions().position(latLngPengguna).title("Lokasi Saya");
    gMap.animateCamera(CameraUpdateFactory.newLatLng(latLngPengguna));
    gMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLngPengguna,14));
    gMap.addMarker(markerOptionsPengguna);

    getDirection();
}

'''

我已经拥有两个标记(添加标记没有问题),但是问题来自于getDirection()。

public void getDirection() {
    new FetchURL(PesananActivity.this).execute(getUrl(markerOptionsPengguna.getPosition(), markerOptionsAmbulans.getPosition(),  "driving"), "driving");
}

这是我的原木猫

2019-12-21 12:50:18.860 1704-1704/com.example.cobamarker E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.cobamarker, PID: 1704
java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.model.LatLng com.google.android.gms.maps.model.MarkerOptions.getPosition()' on a null object reference
    at com.example.cobamarker.PesananActivity.getDirection(PesananActivity.java:181)
    at com.example.cobamarker.PesananActivity.onMapReady(PesananActivity.java:177)
    at com.google.android.gms.maps.zzab.zza(Unknown Source:7)
    at com.google.android.gms.maps.internal.zzaq.onTransact(Unknown Source:38)
    at android.os.Binder.transact(Binder.java:675)
    at cj.b(:com.google.android.gms.dynamite_mapsdynamite@19831065@19.8.31 (100400-0):14)
    at com.google.android.gms.maps.internal.bb.a(:com.google.android.gms.dynamite_mapsdynamite@19831065@19.8.31 (100400-0):4)
    at com.google.maps.api.android.lib6.impl.bi.run(:com.google.android.gms.dynamite_mapsdynamite@19831065@19.8.31 (100400-0):2)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:214)
    at android.app.ActivityThread.main(ActivityThread.java:7076)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)

请帮助我,请原谅我的英语

0 个答案:

没有答案