手机重启后我的谷歌地图应用程序崩溃了

时间:2015-08-09 20:08:20

标签: android google-maps crash maps

嘿伙计^^我做了一个谷歌地图应用程序,使用gps等...它一直很好,直到我重新启动我的手机....每次我尝试启动我的应用程序崩溃:/和我不知道为什么...我认为它不能得到位置或地图不会创造idk但...有人可以帮助我吗?

public class GoogleMapsActivity extends FragmentActivity implements LocationListener, OnMapReadyCallback{

private GoogleMap mMap; // Might be null if Google Play services APK is not available.


private GoogleApiClient mGoogleApiClient;
private LocationRequest myLocationRequest;
public static final String TAG = GoogleMapsActivity.class.getSimpleName();

double latitude2;
double longitude2;



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



    // Macht andere GooglePlayServices einfacher zu benutzen
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApi(LocationServices.API)
            .build();

    // Baut das LocationRequest objekt
    myLocationRequest = LocationRequest.create()
            .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
            .setInterval(10 * 1000)        // 10 seconds, in millisekunden
            .setFastestInterval(1 * 1000); // 1 second, in millisekunden
}










@Override
protected void onResume() {
    super.onResume();
    setUpMapIfNeeded();
}

















private void setUpMapIfNeeded() {
    // Do a null check to confirm that we have not already instantiated the map.
    if (mMap == null) {
        // Try to obtain the map from the SupportMapFragment.
        mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                .getMap();
        // Check if we were successful in obtaining the map.
        if (mMap != null) {
            setUpMap();
        }
    }
}










private void setUpMap() {


}


private void handleNewLocation(Location location) {

    /*Marker flora = mMap.addMarker(new MarkerOptions()
            .position(florakoords)
            .title("Castle")
            .alpha(0.7f));
  */




    Log.d(TAG, location.toString());


    double currentLatitude = location.getLatitude();
    double currentLongitude = location.getLongitude();

    LatLng latLng = new LatLng(currentLatitude, currentLongitude);

    mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));

    double entfernung = vergleicheDistanz(currentLatitude, currentLongitude, latitude2, longitude2);


    //Rundet Entfernung
    entfernung = Math.round(100.0 * entfernung)/100.0;

    String entfernungString = String.valueOf(entfernung);

   // flora.setSnippet(entfernungString);
   // flora.showInfoWindow();




    if (entfernung <= 200.0){


        new AlertDialog.Builder(this).setMessage("Noch 200 Meter bis zum Auto!").setNeutralButton("ok",null).show();


    }

    else {

        new AlertDialog.Builder(this).setMessage("Über 200 meter bis zum Auto").setNeutralButton("ok",null).show();

    }

}





public static float vergleicheDistanz(double latitude, double longitude, double latitude2, double longitude2) {


    Location locationA = new Location("point A");
    locationA.setLatitude(latitude);
    locationA.setLongitude(longitude);

    Location locationB = new Location("point B");
    locationB.setLatitude(latitude2);
    locationB.setLongitude(longitude2);

    float distanz = locationA.distanceTo(locationB);

    return distanz;
}







public void getsavedKoords(){


    Bundle extras = getIntent().getExtras();

    String latitude = extras.getString("latitude");

    String longitude = extras.getString("longitude");

    double latitude2 = Double.valueOf(latitude);
    double longitude2 = Double.valueOf(longitude);

    mMap.addMarker(new MarkerOptions().position(new LatLng(latitude2,longitude2)).title("Saved"));

}






@Override
public void onLocationChanged(Location location) {

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {

}

@Override
public void onProviderEnabled(String provider) {

}

@Override
public void onProviderDisabled(String provider) {

}

@Override
public void onMapReady(GoogleMap googleMap) {

    // Erlaubt google maps meine Location zu nutzen
    mMap.setMyLocationEnabled(true);

    // Ist dazu da das LocationManager aus getSystemService "importiert" wird wofür LocationManager da ist keine ahnung ...
    LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);


    // Create a criteria object to retrieve provider (Braucht man um später seine Location zu bestimmen)
    Criteria criteria = new Criteria();

    String provider = locationManager.getBestProvider(criteria, true);


    Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    if (location == null) {
        // request location update!!
        locationManager.requestLocationUpdates (LocationManager.GPS_PROVIDER,1000, 0,  this);

    }
    else {

        double lat = location.getLatitude();
        double lon = location.getLongitude();

        LatLng latLng = new LatLng(lat,lon);

        getsavedKoords();


        // Show the current location in Google Map (Zeigt die jetzige Location in einer "animation")
        mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));

        // Zoom in the Google Map (Zoomt zu unserer Position, Erschafft danach einen Marker an unserer Position mit der Nachricht "Du bist hier")
        mMap.animateCamera(CameraUpdateFactory.zoomTo(14));



        // Zoom in the Google Map (Zoomt zu unserer Position, Erschafft danach einen Marker an unserer Position mit der Nachricht "Du bist hier")
        mMap.animateCamera(CameraUpdateFactory.zoomTo(14));
    }


}

}

我的主要活动

public class MainActivity extends ActionBarActivity {


public String saved = "false";

private Button ParkMyCar;
private Button BringMeBackToMyCar;
private Button DeleteMyCar;


private SharedPreferences preferences;
private SharedPreferences.Editor preferencesEditor;

private static final String SavedKoordsFile = "savedKoords";
private static final int PREFERENCE_MODE_PRIVATE = 0;

private SharedPreferences FilepreferencesSetting;
private SharedPreferences.Editor FilepreferencesEditor;





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



    ParkMyCar = (Button) findViewById(R.id.ParkMyCar);
    ParkMyCar.setOnClickListener(new View.OnClickListener() {


        @Override


        public void onClick(View ParkMyCar) {



            LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

            Criteria criteria = new Criteria();

            String provider = locationManager.getBestProvider(criteria, true);

            Location myLocation = locationManager.getLastKnownLocation(provider);

            double lat = myLocation.getLatitude();
            double lon = myLocation.getLongitude();



            String savedlatitude = String.valueOf(lat);
            String savedlongitude = String.valueOf(lon);

            saved = "true";

            FilepreferencesSetting = getSharedPreferences(SavedKoordsFile, PREFERENCE_MODE_PRIVATE);
            FilepreferencesEditor = FilepreferencesSetting.edit();


            // preferencesEditor.putString("latitude",savedlatitude); Schreibt die variable in einen textfile
            FilepreferencesEditor.putString("latitude", savedlatitude);
            FilepreferencesEditor.putString("longitude", savedlongitude);
            FilepreferencesEditor.putString("saved", saved);


            //Speichert dad ganze
            FilepreferencesEditor.commit();

            Intent i = new Intent(MainActivity.this, GoogleMapsActivity.class);
            i.putExtra("latitude", savedlatitude);
            i.putExtra("longitude", savedlongitude);
            startActivity(i);


        }
    });




    BringMeBackToMyCar = (Button) findViewById(R.id.BringMeBackToMyCar);
    BringMeBackToMyCar.setOnClickListener(new View.OnClickListener() {


        @Override


        public void onClick(View v) {





           FilepreferencesSetting = getSharedPreferences(SavedKoordsFile, PREFERENCE_MODE_PRIVATE);


            String LoadedLat = FilepreferencesSetting.getString("latitude","DEFAULT");
            String LoadedLon = FilepreferencesSetting.getString("longitude", "DEFAULT");
            String saved = FilepreferencesSetting.getString("saved", "DEFAULT");

            if (saved == "true") {


                FilepreferencesSetting = getSharedPreferences(SavedKoordsFile, PREFERENCE_MODE_PRIVATE);
                FilepreferencesEditor = FilepreferencesSetting.edit();

                Toast.makeText(getApplicationContext(), LoadedLat + " " + LoadedLon, Toast.LENGTH_LONG).show();


                FilepreferencesEditor.putString("LoadedLat", LoadedLat);
                FilepreferencesEditor.putString("LoadedLon", LoadedLon);

                FilepreferencesEditor.commit();

                Intent i = new Intent(MainActivity.this, LoadedGoogleMaps.class);
                i.putExtra("LoadedLat", LoadedLat);
                i.putExtra("LoadedLon", LoadedLon);

                startActivity(i);

            }
            else {


                Toast.makeText(getApplicationContext(), "Keine Position vorhanden!", Toast.LENGTH_SHORT).show();


            }




        }

    });




    DeleteMyCar = (Button)findViewById(R.id.DeleteMyCar);
    DeleteMyCar.setOnClickListener(new View.OnClickListener() {


        @Override


        public void onClick(View v) {


            FilepreferencesSetting = getSharedPreferences(SavedKoordsFile, PREFERENCE_MODE_PRIVATE);
            FilepreferencesEditor = FilepreferencesSetting.edit();

            String saved = FilepreferencesSetting.getString("saved", "DEFAULT");

            if (saved == "true") {




                FilepreferencesEditor.clear();
                FilepreferencesEditor.commit();

                Toast.makeText(getApplicationContext(), "Position gelöscht", Toast.LENGTH_SHORT).show();

                FilepreferencesSetting = getSharedPreferences(SavedKoordsFile, PREFERENCE_MODE_PRIVATE);
                FilepreferencesEditor = FilepreferencesSetting.edit();

                saved = "false";

                FilepreferencesEditor.putString("saved", saved);







            }

            else{

            Toast.makeText(getApplicationContext(), "Die Position wurde schon gelöscht", Toast.LENGTH_SHORT).show();

            }





            }

    });



}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

1 个答案:

答案 0 :(得分:0)

在Manifest中添加以下权限

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />