2017-10-26 1 views
-2

zurückbringen Ich habe ein Android-Programm geschrieben, um Google Karte zu zeigen und den gegenwärtigen Standort zu zeigen.Android kann Google Karte nicht laufen lassen und Netzfehler

Hier ist ein Teil des Codes ist, sehen Sie bitte:

public class MapActivity extends FragmentActivity implements OnMapReadyCallback,LocationListener { 
public static GoogleMap googleMap; 

private Geocoder geoCoder; 
private ArrayAdapter<String> adapter; 
private List<Address> addressSearchList; 
private static final int MY_PERMISSION_ACCESS_COARSE_LOCATION = 11; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.view_map); 
    Log.d("Test","success0"); 
    geoCoder = new Geocoder(MapActivity.this, Locale.getDefault()); 

    TextView textView = (TextView) findViewById(R.id.textTitle); 
    textView.setText(getString(R.string.TITLE_REPORT)); 
    ListView list = (ListView) findViewById(R.id.listView1); 
    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1); 
    list.setAdapter(adapter); 
    list.setOnItemClickListener(new OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, 
           long id) { 
      String addressName = adapter.getItem(position); 
      adapter.clear(); 
      adapter.notifyDataSetChanged(); 
      ListView list = (ListView) findViewById(R.id.listView1); 
      list.setVisibility(View.GONE); 
      if ((addressSearchList == null) || (position >= addressSearchList.size())) 
       return; 
      Address address = addressSearchList.get(position); 
      googleMap.clear(); 
      LatLng latLng = new LatLng(address.getLatitude(), address.getLongitude()); 
      Marker marker = googleMap.addMarker(new MarkerOptions() 
        .position(latLng) 
        .title(addressName) 
        .snippet(getString(R.string.CONFIRM)) 
      ); 
      marker.showInfoWindow(); 
      googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); 
     } 

    }); 

    ImageView imageView = (ImageView) findViewById(R.id.btnCancelSearch); 
    imageView.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      EditText editText = (EditText) findViewById(R.id.editText1); 
      editText.setText(""); 
      editText.clearFocus(); 
      adapter.clear(); 
      adapter.notifyDataSetChanged(); 
      ListView list = (ListView) findViewById(R.id.listView1); 
      list.setVisibility(View.GONE); 
      try { 
       InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
       imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0); 
      } catch (Exception e) { 

      } 
     } 
    }); 

    EditText editText = (EditText) findViewById(R.id.editText1); 
    editText.setOnEditorActionListener(new OnEditorActionListener() { 

     @Override 
     public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { 
      if ((actionId == EditorInfo.IME_ACTION_SEARCH) && (v.getText().length() > 0)) { 
       //Log.d("TEST", "onEditorAction"); 
       try { 
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
        imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0); 
       } catch (Exception e) { 

       } 
       String locationName = v.getText().toString(); 
       new SearchAddressTask(locationName).execute(); 
      } 
      return false; 
     } 
    }); 

    int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this); 
    if (status != ConnectionResult.SUCCESS) { 
     //Log.d("TEST", "not success"); 
    } else { 
     SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentByTag("mapClinic"); 
     Log.d("Test","success1"); 
     fm.getMapAsync(MapActivity.this); 
     Log.d("Test","success2"); 
    } 
} 



@Override 
public void onMapReady(GoogleMap googleMap) { 
    ///final GoogleMap googleMaps=googleMap; 
    // Your code heregoogleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); 


    googleMap.getUiSettings().setMyLocationButtonEnabled(true); 

    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    Criteria criteria = new Criteria(); 
    //Log.d("TEST", clinic.getLongitude() + ":" + clinic.getLatitude()); 
    double latitude = 22.309994; 
    double longitude = 114.226036; 

    LatLng latLng = new LatLng(latitude, longitude); 
    googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); 
    googleMap.animateCamera(CameraUpdateFactory.zoomTo(10)); 

    googleMap.getUiSettings().setRotateGesturesEnabled(false); 
    googleMap.getUiSettings().setCompassEnabled(false); 


    final GoogleMap googleMapF= googleMap; 
    googleMap.setOnMapClickListener(new OnMapClickListener() { 

     @Override 
     public void onMapClick(LatLng point) { 
      googleMapF.clear(); 
      String address = ""; 
      //Geocoder geoCoder = new Geocoder(MapActivity.this, Locale.getDefault()); 
      try { 
       List<Address> addresses = geoCoder.getFromLocation(point.latitude, point.longitude, 1); 
       if (addresses.size() <= 0) 
        return; 
       //address = addresses.get(0).getAddressLine(1); 
       StringBuilder longAddress = new StringBuilder(); 
       String temp; 
       int j = 0; 
       while ((temp = addresses.get(0).getAddressLine(j++)) != null) { 
        longAddress.append(temp); 
       } 
       address = longAddress.toString(); 
       //EditText editText = (EditText) findViewById(R.id.textLocation); 
       //editText.setText(addresses.get(0).getFeatureName()); 

        /*addresses = geoCoder.getFromLocationName("�Ի��{", 10); 
        if (addresses.size() <= 0) { 
         Log.d("TEST", "can't find"); 
        } 
        else { 
         Log.d("TEST", addresses.toString()); 
         Log.d("TEST", "find it"); 
        }*/ 
      } catch (IOException e) { 
       //e.printStackTrace(); 
      } 
      Marker marker = googleMapF.addMarker(new MarkerOptions() 
        .position(point) 
        .title(address) 
        .snippet(getString(R.string.CONFIRM)) 
      ); 
      marker.showInfoWindow(); 
     } 
    }); 

    googleMap.setOnInfoWindowClickListener(new OnInfoWindowClickListener() { 

     @Override 
     public void onInfoWindowClick(Marker marker) { 
      LatLng point = marker.getPosition(); 
      Intent intent = new Intent(); 
      intent.putExtra("LatLng", point); 
      setResult(RESULT_OK, intent); 
      finish(); 
     } 
    }); 

    if(ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
     Log.d("Test","success3...1"); 
     ActivityCompat.requestPermissions(this, new String[] { android.Manifest.permission.ACCESS_COARSE_LOCATION }, 
       MY_PERMISSION_ACCESS_COARSE_LOCATION); 
    } 

    String provider = locationManager.getBestProvider(criteria, false); 
    if (provider == null){ 
     Log.d("Test","success3"); 
     return; 
    } 
    Log.d("Test","success3.1"); 

    Location location = getLastKnownLocationS(); 
    //Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
    Log.d("Test", "success4"); 
    if (location != null) { 
     Log.d("Test", "success5"); 
     LatLng point = new LatLng(location.getLatitude(), location.getLongitude()); 
     Log.d("Test", "success6"); 
     googleMap.clear(); 
     String address = ""; 
     Geocoder geoCoder = new Geocoder(MapActivity.this, Locale.getDefault()); 
     Log.d("Test", "success7"); 
     try { 
      List<Address> addresses = geoCoder.getFromLocation(point.latitude, point.longitude, 1); 
      if (addresses.size() <= 0) 
       return; 
      //address = addresses.get(0).getAddressLine(1); 
      StringBuilder longAddress = new StringBuilder(); 
      String temp; 
      int j = 0; 
      Log.d("Test", "success8"); 
      while ((temp = addresses.get(0).getAddressLine(j++)) != null) { 
       longAddress.append(temp); 
      } 
      address = longAddress.toString(); 
      Log.d("Test", "success9"); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     Log.d("Test", "success10"); 
     Marker marker = googleMap.addMarker(new MarkerOptions() 
       .position(point) 
       .title(address) 
       .snippet(getString(R.string.CONFIRM)) 
     ); 
     Log.d("Test", "success11"); 
     marker.showInfoWindow(); 
     Log.d("Test", "success12.1"); 
     onLocationChanged(location); 
     Log.d("Test", "success12.2"); 
    } 
    locationManager.requestLocationUpdates(provider, 10000, 0, this); 
    Log.d("Test", "success13"); 
} 

private Location getLastKnownLocationS() { 
    LocationManager mLocationManager = (LocationManager)getApplicationContext().getSystemService(LOCATION_SERVICE); 
    List<String> providers = mLocationManager.getProviders(true); 
    Location bestLocation = null; 
    if(ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
     Log.d("Test","success3...2"); 
     ActivityCompat.requestPermissions(this, new String[] { android.Manifest.permission.ACCESS_COARSE_LOCATION }, 
       MY_PERMISSION_ACCESS_COARSE_LOCATION); 
    } 
    for (String provider : providers) { 



     Location l = mLocationManager.getLastKnownLocation(provider); 
     if (l == null) { 
      continue; 
     } 
     if (bestLocation == null || l.getAccuracy() < bestLocation.getAccuracy()) { 
      // Found best last known location: %s", l); 
      bestLocation = l; 
     } 
    } 
    Log.d("Test","success_async"); 
    return bestLocation; 
} 

die Fehlerteile aktualisiert, bitte helfen, danke, für die Fehler in OnLocationChanged Methode, Dank auftreten erinnert.

Fehler Teile aktualisiert, bitte helfen, vielen Dank für die Fehler in OnLocationChanged Methode, Dank

@Override 
    public void onLocationChanged(Location location) { 
     double latitude = location.getLatitude(); 
     double longitude = location.getLongitude(); 
     LatLng latLng = new LatLng(latitude, longitude); 
     Log.d("Test","onLocationChange"); 
     googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); 
     googleMap.animateCamera(CameraUpdateFactory.zoomTo(15)); 
    } 

und Debugger unter Fehler auftreten erinnert:

10-26 08:47:12.493 26361-26361/com.afcd.redtidesystem E/AndroidRuntime: FATAL EXCEPTION: main 
                     Process: com.afcd.redtidesystem, PID: 26361 
                     java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.GoogleMap.moveCamera(com.google.android.gms.maps.CameraUpdate)' on a null object reference 
                      at com.afcd.redtidesystem.MapActivity.onLocationChanged(MapActivity.java:379) 
                      at com.afcd.redtidesystem.MapActivity.onMapReady(MapActivity.java:329) 
                      at com.google.android.gms.maps.zzaj.zza(Unknown Source) 
                      at com.google.android.gms.maps.internal.zzaq.onTransact(Unknown Source) 
                      at android.os.Binder.transact(Binder.java:499) 
                      at gl.b(:[email protected]:20) 
                      at com.google.android.gms.maps.internal.bf.a(:[email protected]:5) 
                      at com.google.maps.api.android.lib6.impl.bc.run(:[email protected]:5) 
                      at android.os.Handler.handleCallback(Handler.java:751) 
                      at android.os.Handler.dispatchMessage(Handler.java:95) 
                      at android.os.Looper.loop(Looper.java:154) 
                      at android.app.ActivityThread.main(ActivityThread.java:6077) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756) 
+0

Sie nicht den entsprechenden Teil des Codes enthalten waren. Die NPE passiert innerhalb der 'onLocationChanged'' Methode. – f1sh

+0

in onMapReady versuchen erste Zeile wie googleMap = googleMap; (globale Variable googleMap = auf Karte bereit Parameter) –

+0

Hallo f1sh, habe den Code aktualisiert, bitte helfen, Danke. – leemack873

Antwort

1

1-Fügen Sie diese Zeile in onCreate() -Methode, wenn Sie Ihre Karte aus dem Kartenfragment erhalten

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

2- Kommentar- Ihre erste Linie in onMapReady() -Methode

+0

Hallo Ikram Hussain, Habe versucht, denselben Fehler zurückzugeben, danke für die Antwort – leemack873

0

i Code geändert haben, versuchen Sie dieses, wenn Fehler in onLocationChangd kommt()

@Override public void OnLocationChanged (Lage, Lage) {

if (Location! = null) {

// Ihr Code hier

} }

0

versuchen diese

CameraUpdate center= 
     CameraUpdateFactory.newLatLng(new LatLng(73.35687586412547, 
               -45.77172998180484)); 
    CameraUpdate zoom=CameraUpdateFactory.zoomTo(10); 

    map.moveCamera(center); 
    map.animateCamera(zoom); 

lassen Sie mich wissen, ob eine Abfrage

Verwandte Themen