尝试调用getMapAsync()方法时应用程序崩溃

时间:2017-08-07 14:00:06

标签: android mysql google-maps google-maps-markers

很抱歉重复提问,但我无法从以前的问题中获得解决方案。我想在谷歌地图上从mysql映射GPS(纬度,经度)。我从mysql得到lat和long但是当我尝试在谷歌地图上映射时,我的应用程序崩溃了。我尝试了很多,但它没有用。它返回

  

null对象引用

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)

2 个答案:

答案 0 :(得分:1)

====$unit::printStack===== #0 in printStack at dpis.sv:9 #1 in DPI function #2 in dpis.sub ====$unit::printStack===== #0 in printStack at dpis.sv:9 #1 in DPI function #2 in <protected code> #3 in abc at dpis.sv:30 #4 in abc 是由此引起的:

NullPointerException

尝试使用((MapFragment) getFragmentManager().findFragmentById(R.id.map)) 代替SupportMapFragment

getFragmentManager

答案 1 :(得分:0)

在地图准备好后添加标记,如

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