對不起,重複的問題,但我不能從以前的問題得到解決方案。我想在谷歌地圖上從MySQL映射GPS(緯度,經度)。我從MySQL獲得緯度和長度,但當我嘗試在谷歌地圖上映射它時,我的應用程序崩潰。我嘗試了很多,但它不工作。它返回嘗試調用getMapAsync()方法時應用程序崩潰
空對象引用
錯誤logcat中。它不可能是以前的空對象引用。
我的班級RecGPS代碼在這裏。
public class RecGPS extends AppCompatActivity{
private GoogleMap mMap;
Button btn;
private ProgressDialog loading;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rec_gps);
btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
recGps();
}
});
}
private void recGps() {
loading = ProgressDialog.show(this, "Please wait ......", "fetching....", false, false);
String url = Config.URL_CARD_INFO;
StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
loading.dismiss();
showJSON(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(RecGPS.this, error.getMessage().toString(), Toast.LENGTH_LONG).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
private void showJSON(String response) {
String latitude = "";
String longitude = "";
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray result = jsonObject.getJSONArray(Config.TAG_JSON_ARRAY);
JSONObject collegeData = result.getJSONObject(0);
latitude = collegeData.getString(Config.TAG_ISSUE_DATE);
longitude = collegeData.getString(Config.TAG_Expiry_DATE);
} catch (JSONException e) {
e.printStackTrace();
}
Toast.makeText(this,latitude+longitude,Toast.LENGTH_LONG).show();
// ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMapAsync(this);
((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap googleMap) {
mMap=googleMap;
}
});
mMap.addMarker(new MarkerOptions().position(new LatLng(Double.parseDouble(longitude), Double.parseDouble(latitude))));
}
}
我.XML文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_rec_gps"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.hp.getgpscoordinates.RecGPS">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="400dp">
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map"
android:clickable="true"
android:name="com.google.android.gms.maps.SupportMapFragment"
/>
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btn"/>
</LinearLayout>
logcat的
Attempt to invoke virtual method 'void com.google.android.gms.maps.MapFragment.getMapAsync(com.google.android.gms.maps.OnMapReadyCallback)' on a null object reference
at com.example.hp.getgpscoordinates.RecGPS.showJSON(RecGPS.java:92)
at com.example.hp.getgpscoordinates.RecGPS.access$200(RecGPS.java:25)
at com.example.hp.getgpscoordinates.RecGPS$2.onResponse(RecGPS.java:56)
at com.example.hp.getgpscoordinates.RecGPS$2.onResponse(RecGPS.java:52)
at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60)
at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7325)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
添加您的logcat報告請 –
添加崩潰日誌質疑 – USKMobility
哪裏是你的'onMapReady()'方法,和其他Google Map生命週期方法?沒有冒犯,但這個代碼有一種氣味。我的建議是:添加斷點並精確定位代碼崩潰的位置。 –