2014-12-10 107 views
0

我創建了一個概念應用,抓取三個位置並在Google地圖上顯示它們:我的學校,我的家和我的當前位置。android當前位置導致應用崩潰

該應用程序曾經工作,但現在它每當我要求它拉當前位置時崩潰。上週我沒有對該應用進行任何更改,但現在它無法正常工作。奇怪的是,它仍然會拉我的學校和家庭。

import android.app.Activity; 
import android.content.Context; 
import android.location.Criteria; 
import android.location.Location; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.location.Location; 
import com.google.android.gms.location.LocationListener; 
import com.google.android.gms.maps.CameraUpdate; 
import com.google.android.gms.maps.CameraUpdateFactory; 
import com.google.android.gms.maps.GoogleMap; 
import com.google.android.gms.maps.MapFragment; 
import com.google.android.gms.maps.model.LatLng; 
import com.google.android.gms.maps.model.MarkerOptions; 




//Loosely based on an App tutorial from Youtube by ram goud. 

public class MapActivity extends Activity implements android.location.LocationListener { 

    private final LatLng LOCATION_HOME = new LatLng(45.310488, -93.536989); 
    private final LatLng LOCATION_SCHOOL = new LatLng(44.972586, -93.283689); 
    protected LocationManager locationManager; 
    protected LocationListener locationListener; 
    protected boolean gps_enabled, network_enabled; 
    Button homeButton; 
    Button schoolButton; 
    Button currentLocation; 
    private double lat; 
    private double lng; 
    private final LatLng LOCATION_CURRENT = null; 

    private GoogleMap map; 






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



     map =((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap(); 
     map.setMyLocationEnabled(true); 
     map.getUiSettings().setRotateGesturesEnabled(false); //I dislike this feature. That is why it has been removed. 

//  Criteria criteria = new Criteria(); 
//  String provider = locationManager.getBestProvider(criteria, gps_enabled); 
//  final Location myLocation = locationManager.getLastKnownLocation(provider); 

     locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); 


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

      @Override 
      public void onClick(View v) { 
       map.setMapType(GoogleMap.MAP_TYPE_TERRAIN); //MAP_TYPE_TERRAIN sets the map displayed as just a normal Google Map image. 
       CameraUpdate update = CameraUpdateFactory.newLatLngZoom(LOCATION_HOME, 14); // newLatLngZoom is for zooming. the # represents the distance. 21 furthest & 1 being closest. 
       map.animateCamera(update); 
       map.addMarker(new MarkerOptions().position(LOCATION_HOME).title("Hello, Home")); 
      } 

     }); 
     schoolButton = (Button)findViewById(R.id.school_button); 
     schoolButton.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       map.setMapType(GoogleMap.MAP_TYPE_SATELLITE); //MAP_TYPE_SATELLITE sets the map displayed as a sat image 
       CameraUpdate update = CameraUpdateFactory.newLatLngZoom(LOCATION_SCHOOL, 16); // newLatLngZoom is for zooming. the # represents the distance. 21 furthest & 1 being closest. 
       map.addMarker(new MarkerOptions().position(LOCATION_SCHOOL).title("Hello, School")); 
       map.animateCamera(update); 

      } 
     }); 

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

      @Override 
      public void onClick(View v) { 
       GoToCurrent(); 

       } 


     }); 


    } 

    private void GoToCurrent() { //Method created to find current lat/long of user. 
     LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 
     map.setMapType(GoogleMap.MAP_TYPE_HYBRID); 
     Criteria criteria = new Criteria(); 
     String provider = locationManager.getBestProvider(criteria, gps_enabled); 
     Location myLocation = locationManager.getLastKnownLocation(provider); 

     double lati = myLocation.getLatitude(); 
     double longi = myLocation.getLongitude(); 
     LatLng latlng = new LatLng(lati, longi); 
     map.moveCamera(CameraUpdateFactory.newLatLng(latlng)); 
     map.animateCamera(CameraUpdateFactory.zoomTo(20)); 
     map.addMarker(new MarkerOptions().position(new LatLng(lati, longi)).title("You are here")); 
    } 


    @Override 
    public void onLocationChanged(Location location) { 


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

    } 

    @Override 
    public void onProviderEnabled(String provider) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onProviderDisabled(String provider) { 
     // TODO Auto-generated method stub 

    } 




} 

//這是清單,以防萬一有人好奇。

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.googlemaps" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="11" 
     android:targetSdkVersion="19" /> 



    <uses-permission android:name="com.google.android.gms.version"/> 

    <uses-permission android:name="android.permission.INTERNET"/> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/> 
    <!-- The following two permissions are not required to use 
     Google Maps Android API v2, but are recommended. --> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> 

    <!-- The following two permissions are not required to use Google Maps Android API v2, but are recommended. --> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name=".MapActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

     <meta-data 
      android:name="com.google.android.maps.v2.API_KEY" 
      android:value="xxxxxxxxxxxxxxxxxxxxxx-xxxxxxxx" /> <!-- API key --> 
       <meta-data 
       android:name="com.google.android.gms.version" 
      android:value="@integer/google_play_services_version"/> 
    </application> 

</manifest> 
+0

您可以添加堆棧跟蹤嗎? – 2014-12-10 21:38:47

回答

0

你不需要調用它的任何方法

Location myLocation = locationManager.getLastKnownLocation(provider); 

,如果你看一下文檔,發現前檢查,如果你的價值myLocation變量爲空或here

你會看到這個函數可以返回null

public Location getLastKnownLocation(String提供者)在API中添加 級別1

返回一個位置,指示從給定提供者獲取的最後一個已知位置 修復的數據。

這可以在不啓動提供程序的情況下完成。請注意,此 位置可能已過期,例如,如果該設備已關閉並移動到其他位置,則會關閉該位置 。

如果提供者當前被禁用,則返回null。參數 提供商提供者的名稱返回

the last known location for the provider, or **null** 

拋出SecurityException若沒有合適的權限是目前 IllegalArgumentException - 如果提供者爲null或不存在

-1

那麼奇怪的是,它的工作沒有。代碼沒有改變。它只是...的作品!

+0

它的工作原因,是因爲getLastKnownLocation(provider)有時只返回null。如果你看看上面的文檔,你會發現,該方法__can__返回null。如果你忽略這個事實,那麼在某個時候,應用程序__將會像之前那樣崩潰。很容易就放棄似乎已經改正它的東西,但是如果你想學習某些東西,那麼檢查null條件並顯示一個toast,或者將某些東西打印到日誌或任何東西,如果它爲null和__don '調用空對象上的任何方法。最糟糕的情況下,你永遠不會看到吐司/日誌消息! – nPn 2014-12-12 03:22:40