在GoogleMap API v2中获取我的位置片段中的空指针

时间:2017-06-20 09:36:10

标签: java android google-maps google-maps-android-api-2 google-maps-api-2

我正试图通过点击保存按钮获取我的位置&存储在 数据库中。

问题:

当我试图获取我的位置时,它会显示空指针异常。

有人能告诉我代码中有什么问题吗?

我的代码:

var nicEditorColorButton = nicEditorAdvancedButton.extend({ 
  addPane : function() {
  var colorList = {0 : '000000',1 : 'FFFFFF'};  /* here goes color list */
  var colorItems = new bkElement('DIV').setStyle({width: '270px'});

  for(var g in colorList) {
    var colorCode = '#'+colorList[g];
    var colorSquare = new bkElement('DIV').setStyle({'cursor' : 'pointer', 'height' : '15px', 'float' : 'left'}).appendTo(colorItems);
    var colorBorder = new bkElement('DIV').setStyle({border: '2px solid '+colorCode}).appendTo(colorSquare);
    var colorInner = new bkElement('DIV').setStyle({backgroundColor : colorCode, overflow : 'hidden', width : '11px', height : '11px'}).addEvent('click',this.colorSelect.closure(this,colorCode)).addEvent('mouseover',this.on.closure(this,colorBorder)).addEvent('mouseout',this.off.closure(this,colorBorder,colorCode)).appendTo(colorBorder);

    if(!window.opera) {
      colorSquare.onmousedown = colorInner.onmousedown = bkLib.cancelEvent;
    }

  }
  this.pane.append(colorItems.noSelect());  
}});

错误日志:

public class OthersFragment extends Fragment implements OnMapReadyCallback,GoogleApiClient.ConnectionCallbacks,
            GoogleApiClient.OnConnectionFailedListener {
         View view;

        private GoogleMap mMap;
         GoogleApiClient mGoogleApiClient;
        Double latitude;
        Double longitude;
        String strplace;

        public static OthersFragment newInstance() {
            OthersFragment fragment = new OthersFragment();
            return fragment;
        }

        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    //        mGoogleApiClient = new GoogleApiClient.Builder(getContext())
    //                .addApi(LocationServices.API)
    //                .addConnectionCallbacks(this)
    //                .addOnConnectionFailedListener(this)
    //                .build();

            view = inflater.inflate(R.layout.fragment_other, null, false);
            SupportMapFragment mapFragment = (SupportMapFragment) this.getChildFragmentManager().findFragmentById(R.id.map);
            mapFragment.getMapAsync(this);
            return view;
        }

        @Override
        public void onMapReady(GoogleMap googleMap) {
            mGoogleApiClient = new GoogleApiClient.Builder(getContext())
                    .addApi(LocationServices.API)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .build();

            mMap = googleMap;

            // Add a marker in Sydney and move the camera
            LatLng sydney = new LatLng(-34, 151);
            mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
            mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));

            mMap.setMyLocationEnabled(true);

            mMap.setOnMyLocationButtonClickListener(new GoogleMap.OnMyLocationButtonClickListener() {
                @Override
                public boolean onMyLocationButtonClick() {

                    Location mylocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
                    ShowPopUp(mylocation);
                    return true;
                }
            });

            googleMap.getUiSettings().setZoomControlsEnabled(true);
            googleMap.getUiSettings().setAllGesturesEnabled(true);


        }

        private void ShowPopUp(final Location mylocation) {

            int popupWidth = 300;
            int popupHeight = 150;

            // Inflate the popup_layout.xml
            LinearLayout viewGroup = (LinearLayout)view.findViewById(R.id.popup);
            LayoutInflater layoutInflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View layout = layoutInflater.inflate(R.layout.popup, viewGroup);

            // Creating the PopupWindow
            final PopupWindow popup = new PopupWindow();
            popup.setContentView(layout);
            popup.setWidth(popupWidth);
            popup.setHeight(popupHeight);
            popup.setFocusable(true);

            // Some offset to align the popup a bit to the right, and a bit down, relative to button's position.
            int OFFSET_X = 30;
            int OFFSET_Y = 30;

            // Clear the default translucent background
            popup.setBackgroundDrawable(new BitmapDrawable());

            // Displaying the popup at the specified location, + offsets.
            popup.showAtLocation(layout, Gravity.NO_GRAVITY, + OFFSET_X,  OFFSET_Y);

            // Getting a reference to Close button, and close the popup when clicked.
            Button save = (Button) layout.findViewById(R.id.save);
            Button retrieve = (Button) layout.findViewById(R.id.retrieve);
            final EditText place = (EditText)layout.findViewById(R.id.place);
            final EditText phone = (EditText)layout.findViewById(R.id.phone);

            save.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                      String action = "save";
                      String strplace = place.getText().toString();
                      String strphone = phone.getText().toString();
                    new storePoints().execute(strplace,String.valueOf(mylocation.getLatitude()), String.valueOf(mylocation.getLongitude()), action);
                    popup.dismiss();

                }
            });

            retrieve.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String action = "retrieve";
                    strplace = place.getText().toString();
                    String strphone = phone.getText().toString();
                    new retrievePoints().execute(strplace, action);
                    popup.dismiss();
                }
            });

        }


        @Override
        public void onConnected(Bundle bundle) {

        }

        @Override
        public void onConnectionSuspended(int i) {

        }

        @Override
        public void onConnectionFailed(ConnectionResult connectionResult) {

        }

        private class storePoints extends AsyncTask<String, Void, String> {

            @Override
            protected String doInBackground(String... params) {

                try {
                    String place = (String) params[0];
                  //  String phone = (String) params[1];
                    String latitude = (String) params[1];
                    String longitude = (String) params[2];
                    String action = (String) params[3];

                    String link = "http://192.168.0.152/prestige/saveCoord.php";
                    // String link = "http://10.0.2.2/coordinates/coord.php";

                    String data = URLEncoder.encode("station_name", "UTF-8") + "=" + URLEncoder.encode(place, "UTF-8");
                   // data += "&" + URLEncoder.encode("phone", "UTF-8") + "=" + URLEncoder.encode(phone, "UTF-8");
                    data += "&" + URLEncoder.encode("latitude", "UTF-8") + "=" + URLEncoder.encode(latitude, "UTF-8");
                    data += "&" + URLEncoder.encode("longitude", "UTF-8") + "=" + URLEncoder.encode(longitude, "UTF-8");

                    URL url = new URL(link);
                    URLConnection conn = url.openConnection();

                    conn.setDoOutput(true);
                    OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());

                    wr.write(data);
                    wr.flush();

                    BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));

                    StringBuilder sb = new StringBuilder();
                    String line = null;

                    // Read Server Response
                    while ((line = reader.readLine()) != null) {
                        sb.append(line);
                        break;
                    }
                    return sb.toString();

                } catch (Exception e) {
                    return new String("Exception: " + e.getMessage());
                }

            }

            @Override
            protected void onPostExecute(String result) {

                Toast.makeText(OthersFragment.this.getContext(), result, Toast.LENGTH_LONG).show();


            }
        }


        private class retrievePoints extends AsyncTask<String, Void, String>{


            @Override
            protected String doInBackground(String... params) {

                try {
                    String place = (String) params[0];
                    String phone = (String) params[1];

                    String link = "http://192.168.0.152/prestige/retCoord.php";
                    // String link = "http://10.0.2.2/coordinates/retCoord.php";

                    String data = URLEncoder.encode("station_name", "UTF-8") + "=" + URLEncoder.encode(place, "UTF-8");
                    data += "&" + URLEncoder.encode("phone", "UTF-8") + "=" + URLEncoder.encode(phone, "UTF-8");


                    URL url = new URL(link);
                    URLConnection conn = url.openConnection();

                    conn.setDoOutput(true);
                    OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());

                    wr.write(data);
                    wr.flush();

                    BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));

                    StringBuilder sb = new StringBuilder();
                    String line = null;

                    // Read Server Response
                    while ((line = reader.readLine()) != null) {
                        sb.append(line);
                        break;
                    }
                    return sb.toString();

                } catch (Exception e) {
                    return new String("Exception: " + e.getMessage());
                }

            }

            @Override
            protected void onPostExecute(String result) {

                //  Toast.makeText(MainActivity.this,result,Toast.LENGTH_LONG).show();

                try {

                    mMap.clear(); // clear all the markers on the map.
                    JSONArray loc_arr = new JSONArray(result);
                    for (int i = 0; i < loc_arr.length(); i++){
                        JSONObject loc_obj = loc_arr.getJSONObject(i);
                        Double  latitude = Double.valueOf((loc_obj.getString("latitude")));
                        Double  longitude = Double.valueOf((loc_obj.getString("longitude")));
                        String place = loc_obj.getString("station_name");
                        //                Toast.makeText(MainActivity.this, "latitude: "+latitude +"\t Longitude: " + longitude+"\t Place: "+place, Toast.LENGTH_SHORT).show();


                        Marker marker= mMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).title("Place: "+place).snippet("Latitude: " +latitude+",Longitude: "+longitude));
                        marker.showInfoWindow();

                        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude,longitude),28));

                    }

                } catch (JSONException e) {

                    e.printStackTrace();

                }
            }
        }


    }

2 个答案:

答案 0 :(得分:0)

例如,尝试获取如下所示的最后一个位置,

public class MapExample extends FragmentActivity implements OnMapReadyCallback,
    GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener,
    LocationListener {

private GoogleMap mMap;
GoogleApiClient mGoogleApiClient;
Location mLastLocation;
Marker mCurrLocationMarker;
LocationRequest mLocationRequest;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);

    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        checkLocationPermission();
    }

    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}


@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);

    //Initialize Google Play Services
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.ACCESS_FINE_LOCATION)
                == PackageManager.PERMISSION_GRANTED) {
            buildGoogleApiClient();
            mMap.setMyLocationEnabled(true);
        }
    }
    else {
        buildGoogleApiClient();
        mMap.setMyLocationEnabled(true);
    }
}

protected synchronized void buildGoogleApiClient() {
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();
    mGoogleApiClient.connect();
}

@Override
public void onConnected(Bundle bundle) {

    mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(1000);
    mLocationRequest.setFastestInterval(1000);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
    if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.ACCESS_FINE_LOCATION)
            == PackageManager.PERMISSION_GRANTED) {
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
    }
else{
  mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
    if (mLastLocation != null) {
        // work with your last location.
    } else { 
        Toast.makeText(this, R.string.no_location, Toast.LENGTH_LONG).show();
    } 
}

}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onLocationChanged(Location location) {

        mLastLocation = location;
        if (mCurrLocationMarker != null) {
            mCurrLocationMarker.remove();
        }

        //Place current location marker
        LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
        MarkerOptions markerOptions = new MarkerOptions();
        markerOptions.position(latLng);
        markerOptions.title("Current Position");
        markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
        mCurrLocationMarker = mMap.addMarker(markerOptions);

        //move map camera
        mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
        mMap.animateCamera(CameraUpdateFactory.zoomTo(11));

        //stop location updates
        if (mGoogleApiClient != null) {
            LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
        }

}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {

}

public static final int MY_PERMISSIONS_REQUEST_LOCATION = 99;
public boolean checkLocationPermission(){
    if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.ACCESS_FINE_LOCATION)
            != PackageManager.PERMISSION_GRANTED) {

        // Asking user if explanation is needed
        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                Manifest.permission.ACCESS_FINE_LOCATION)) {

            // Show an explanation to the user *asynchronously* -- don't block
            // this thread waiting for the user's response! After the user
            // sees the explanation, try again to request the permission.

            //Prompt the user once explanation has been shown
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                    MY_PERMISSIONS_REQUEST_LOCATION);


        } else {
            // No explanation needed, we can request the permission.
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                    MY_PERMISSIONS_REQUEST_LOCATION);
        }
        return false;
    } else {
        return true;
    }
}

@Override
public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {
    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_LOCATION: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // permission was granted. Do the
                // contacts-related task you need to do.
                if (ContextCompat.checkSelfPermission(this,
                        Manifest.permission.ACCESS_FINE_LOCATION)
                        == PackageManager.PERMISSION_GRANTED) {

                    if (mGoogleApiClient == null) {
                        buildGoogleApiClient();
                    }
                    mMap.setMyLocationEnabled(true);
                }

            } else {

                // Permission denied, Disable the functionality that depends on this permission.
                Toast.makeText(this, "permission denied", Toast.LENGTH_LONG).show();
            }
            return;
        }

        // other 'case' lines to check for other permissions this app might request.
        // You can add here other case statements according to your requirement.
    }
}
}

答案 1 :(得分:0)

我添加了这个,它对我有用。

 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
     view = inflater.inflate(R.layout.fragment_entry, null, false);

       //added

    if (!isGooglePlayServicesAvailable()) {

        Toast.makeText(getContext(), "Google Play Services is not available", Toast.LENGTH_LONG).show();

        getActivity().finish();
    }

    mGoogleApiClient = new GoogleApiClient.Builder(getContext())
            .addApi(LocationServices.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();

    SupportMapFragment mapFragment = (SupportMapFragment) this.getChildFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

    return view;
}

这也是我添加的

@Override
public void onStart() {
    super.onStart();

    mGoogleApiClient.connect();
}

@Override
public void onStop() {
    super.onStop();

    mGoogleApiClient.disconnect();

}

private boolean isGooglePlayServicesAvailable() {
    int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getContext());
    if (ConnectionResult.SUCCESS == status) {
        return true;
    } else {
        GooglePlayServicesUtil.getErrorDialog(status, getActivity(), 0).show();
        Toast.makeText(getContext(), "Google Play Services is not Available", Toast.LENGTH_LONG).show();
        return false;
    }
}