2016-09-29 10 views
-1

ために近くの場所を表示しようとしています() にNullPointer例外私はこれは私が査読しているが、私は、検索ボックスに、ATMや学校のような名前を入力すると、それは私にgoogleMap.clear上</p> <p>PlacesDisplayTask.javaでエラーになるリンクである固定位置

http://javapapers.com/android/find-places-nearby-in-google-maps-using-google-places-apiandroid-app/

私は別のコードを試し

これは

あります

private GoogleApiClient client; 
 

 
    @Override 
 
    protected void onCreate(Bundle savedInstanceState) { 
 
     super.onCreate(savedInstanceState); 
 
     setContentView(R.layout.activity_neighborhood); 
 
     // Obtain the SupportMapFragment and get notified when the map is ready to be used. 
 
    /* SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() 
 
       .findFragmentById(R.id.map); 
 
     mapFragment.getMapAsync(this); 
 
*/ 
 

 
     // Array of place types 
 
     mPlaceType = getResources().getStringArray(R.array.place_type); 
 

 
     // Array of place type names 
 
     mPlaceTypeName = getResources().getStringArray(R.array.place_type_name); 
 

 
     // Creating an array adapter with an array of Place types 
 
     // to populate the spinner 
 
     ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, mPlaceTypeName); 
 

 
     // Getting reference to the Spinner 
 
     mSprPlaceType = (Spinner) findViewById(R.id.spr_place_type); 
 

 
     // Setting adapter on Spinner to set place types 
 
     mSprPlaceType.setAdapter(adapter); 
 

 
     Button btnFind; 
 

 
     // Getting reference to Find Button 
 
     btnFind = (Button) findViewById(R.id.btn_find); 
 

 

 
     // Getting Google Play availability status 
 
     int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext()); 
 

 

 
     if (status != ConnectionResult.SUCCESS) { // Google Play Services are not available 
 

 
      int requestCode = 10; 
 
      Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode); 
 
      dialog.show(); 
 

 
     } else { // Google Play Services are available 
 

 
      // Getting reference to the SupportMapFragment 
 
      SupportMapFragment fragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); 
 

 
      // Getting Google Map 
 
      fragment.getMapAsync(this); 
 

 
      // Enabling MyLocation in Google Map 
 

 

 
      // Getting LocationManager object from System Service LOCATION_SERVICE 
 
      LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 
 

 
      // Creating a criteria object to retrieve provider 
 
      Criteria criteria = new Criteria(); 
 

 
      // Getting the name of the best provider 
 
      String provider = locationManager.getBestProvider(criteria, true); 
 

 
      // Getting Current Location From GPS 
 
      if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
 
       // TODO: Consider calling 
 
       // ActivityCompat#requestPermissions 
 
       // here to request the missing permissions, and then overriding 
 
       // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
 
       //           int[] grantResults) 
 
       // to handle the case where the user grants the permission. See the documentation 
 
       // for ActivityCompat#requestPermissions for more details. 
 
       return; 
 
      } 
 
      Location location = locationManager.getLastKnownLocation(provider); 
 

 
      if (location != null) { 
 
       onLocationChanged(location); 
 
      } 
 

 
      locationManager.requestLocationUpdates(provider,this); 
 

 
      // Setting click event lister for the find button 
 
      btnFind.setOnClickListener(new View.OnClickListener() { 
 

 
       @Override 
 
       public void onClick(View v) { 
 

 

 
        int selectedPosition = mSprPlaceType.getSelectedItemPosition(); 
 
        String type = mPlaceType[selectedPosition]; 
 

 

 
        StringBuilder sb = new StringBuilder("https://maps.googleapis.com/maps/api/place/nearbysearch/json?"); 
 
        sb.append("location=" + mLatitude + "," + mLongitude); 
 
        sb.append("&radius=5000"); 
 
        sb.append("&types=" + type); 
 
        sb.append("&sensor=true"); 
 
        sb.append("&key=YOUR_API_KEY"); 
 

 

 
        // Creating a new non-ui thread task to download Google place json data 
 
        PlacesTask placesTask = new PlacesTask(); 
 

 
        // Invokes the "doInBackground()" method of the class PlaceTask 
 
        placesTask.execute(sb.toString()); 
 

 

 
       } 
 
      }); 
 

 
     } 
 

 

 
     // ATTENTION: This was auto-generated to implement the App Indexing API. 
 
     // See https://g.co/AppIndexing/AndroidStudio for more information. 
 
     client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build(); 
 
    } 
 

 
    private String downloadUrl(String strUrl) throws IOException { 
 
     String data = ""; 
 
     InputStream iStream = null; 
 
     HttpURLConnection urlConnection = null; 
 
     try { 
 
      URL url = new URL(strUrl); 
 

 

 
      // Creating an http connection to communicate with url 
 
      urlConnection = (HttpURLConnection) url.openConnection(); 
 

 
      // Connecting to url 
 
      urlConnection.connect(); 
 

 
      // Reading data from url 
 
      iStream = urlConnection.getInputStream(); 
 

 
      BufferedReader br = new BufferedReader(new InputStreamReader(iStream)); 
 

 
      StringBuffer sb = new StringBuffer(); 
 

 
      String line = ""; 
 
      while ((line = br.readLine()) != null) { 
 
       sb.append(line); 
 
      } 
 

 
      data = sb.toString(); 
 

 
      br.close(); 
 

 
     } catch (Exception e) { 
 
      Log.d("Exception while downloading url", e.toString()); 
 
     } finally { 
 
      iStream.close(); 
 
      urlConnection.disconnect(); 
 
     } 
 

 
     return data; 
 
    } 
 

 
    /** A class, to download Google Places */ 
 
    private class PlacesTask extends AsyncTask<String, Integer, String> { 
 

 
     String data = null; 
 

 
     // Invoked by execute() method of this object 
 
     @Override 
 
     protected String doInBackground(String... url) { 
 
      try { 
 
       data = downloadUrl(url[0]); 
 
      } catch (Exception e) { 
 
       Log.d("Background Task", e.toString()); 
 
      } 
 
      return data; 
 
     } 
 

 
     // Executed after the complete execution of doInBackground() method 
 
     @Override 
 
     protected void onPostExecute(String result) { 
 
      ParserTask parserTask = new ParserTask(); 
 

 
      // Start parsing the Google places in JSON format 
 
      // Invokes the "doInBackground()" method of the class ParseTask 
 
      parserTask.execute(result); 
 
     } 
 

 
    } 
 

 
    /** A class to parse the Google Places in JSON format */ 
 
    private class ParserTask extends AsyncTask<String, Integer, List<HashMap<String, String>>> { 
 

 
     JSONObject jObject; 
 

 
     // Invoked by execute() method of this object 
 
     @Override 
 
     protected List<HashMap<String, String>> doInBackground(String... jsonData) { 
 

 
      List<HashMap<String, String>> places = null; 
 
      PlaceJSONParser placeJsonParser = new PlaceJSONParser(); 
 

 
      try { 
 
       jObject = new JSONObject(jsonData[0]); 
 

 
       /** Getting the parsed data as a List construct */ 
 
       places = placeJsonParser.parse(jObject); 
 

 
      } catch (Exception e) { 
 
       Log.d("Exception", e.toString()); 
 
      } 
 
      return places; 
 
     } 
 

 
     // Executed after the complete execution of doInBackground() method 
 
     @Override 
 
     protected void onPostExecute(List<HashMap<String, String>> list) { 
 

 
      // Clears all the existing markers 
 
      mGoogleMap.clear(); 
 

 
      for (int i = 0; i < list.size(); i++) { 
 

 
       // Creating a marker 
 
       MarkerOptions markerOptions = new MarkerOptions(); 
 

 
       // Getting a place from the places list 
 
       HashMap<String, String> hmPlace = list.get(i); 
 

 
       // Getting latitude of the place 
 
       double lat = Double.parseDouble(hmPlace.get("lat")); 
 

 
       // Getting longitude of the place 
 
       double lng = Double.parseDouble(hmPlace.get("lng")); 
 

 
       // Getting name 
 
       String name = hmPlace.get("place_name"); 
 

 
       // Getting vicinity 
 
       String vicinity = hmPlace.get("vicinity"); 
 

 
       LatLng latLng = new LatLng(lat, lng); 
 

 
       // Setting the position for the marker 
 
       markerOptions.position(latLng); 
 

 
       // Setting the title for the marker. 
 
       //This will be displayed on taping the marker 
 
       markerOptions.title(name + " : " + vicinity); 
 

 
       // Placing a marker on the touched position 
 
       mGoogleMap.addMarker(markerOptions); 
 

 
      } 
 

 
     } 
 

 
    } 
 

 
    /** 
 
    * Manipulates the map once available. 
 
    * This callback is triggered when the map is ready to be used. 
 
    * This is where we can add markers or lines, add listeners or move the camera. In this case, 
 
    * we just add a marker near Sydney, Australia. 
 
    * If Google Play services is not installed on the device, the user will be prompted to install 
 
    * it inside the SupportMapFragment. This method will only be triggered once the user has 
 
    * installed Google Play services and returned to the app. 
 
    */ 
 
    @Override 
 
    public void onMapReady(GoogleMap googleMap) { 
 
     googleMap = googleMap; 
 

 
     // Add a marker in Sydney and move the camera 
 
     LatLng sydney = new LatLng(19.0330488, 73.0296625); 
 
     googleMap.addMarker(new MarkerOptions().position(sydney).title("CBD")); 
 
     googleMap.moveCamera(CameraUpdateFactory.newLatLng(sydney)); 
 
     googleMap.setMinZoomPreference(15.0f); 
 
     googleMap.setMaxZoomPreference(20.0f); 
 
     if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
 
      // TODO: Consider calling 
 
      // ActivityCompat#requestPermissions 
 
      // here to request the missing permissions, and then overriding 
 
      // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
 
      //           int[] grantResults) 
 
      // to handle the case where the user grants the permission. See the documentation 
 
      // for ActivityCompat#requestPermissions for more details. 
 
      return; 
 
     } 
 
     googleMap.setMyLocationEnabled(true); 
 
    } 
 

 
    @Override 
 
    public void onLocationChanged(Location location) { 
 
     mLatitude = location.getLatitude(); 
 
     mLongitude = location.getLongitude(); 
 
     LatLng latLng = new LatLng(mLatitude, mLongitude); 
 

 
     mGoogleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); 
 
     mGoogleMap.animateCamera(CameraUpdateFactory.zoomTo(12)); 
 
    } 
 

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

 
     // ATTENTION: This was auto-generated to implement the App Indexing API. 
 
     // See https://g.co/AppIndexing/AndroidStudio for more information. 
 
     client.connect(); 
 
     Action viewAction = Action.newAction(
 
       Action.TYPE_VIEW, // TODO: choose an action type. 
 
       "Neighborhood Page", // TODO: Define a title for the content shown. 
 
       // TODO: If you have web page content that matches this app activity's content, 
 
       // make sure this auto-generated web page URL is correct. 
 
       // Otherwise, set the URL to null. 
 
       Uri.parse("http://host/path"), 
 
       // TODO: Make sure this auto-generated app URL is correct. 
 
       Uri.parse("android-app://com.example.soulsystem_4.myapplication/http/host/path") 
 
     ); 
 
     AppIndex.AppIndexApi.start(client, viewAction); 
 
    } 
 

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

 
     // ATTENTION: This was auto-generated to implement the App Indexing API. 
 
     // See https://g.co/AppIndexing/AndroidStudio for more information. 
 
     Action viewAction = Action.newAction(
 
       Action.TYPE_VIEW, // TODO: choose an action type. 
 
       "Neighborhood Page", // TODO: Define a title for the content shown. 
 
       // TODO: If you have web page content that matches this app activity's content, 
 
       // make sure this auto-generated web page URL is correct. 
 
       // Otherwise, set the URL to null. 
 
       Uri.parse("http://host/path"), 
 
       // TODO: Make sure this auto-generated app URL is correct. 
 
       Uri.parse("android-app://com.example.soulsystem_4.myapplication/http/host/path") 
 
     ); 
 
     AppIndex.AppIndexApi.end(client, viewAction); 
 
     client.disconnect(); 
 
    } 
 
}

私は第四locationManager.requestLocationUpdatesにおけるパラメータ(プロバイダ、20000、0、)何を渡すshoul。

それは常に場所リスナーにキャスト4番目のパラメータを言っていると私はそれはまだそれは私にエラーを与えるキャストするとandroid.location.LocationListener

答えて

関連する問題

 関連する問題