尝试获取当前位置时出现NullPointerException

时间:2012-02-27 08:41:26

标签: android

我正在尝试设置位置但获取NullPointerException,我已提供android.permission.ACCESS_COARSE_LOCATIONandroid.permission.ACCESS_FINE_LOCATION
在清单文件中,我的源代码如下,我正在获取Null指针 List<Address> addresses = new Geocoder(LocationDemo.this,Locale.getDefault()).getFromLocation(currentLocation.getLatitude(), currentLocation.getLongitude(), 1);

我的编码如下

import java.io.IOException;
import java.util.*;
import android.widget.*;
import android.app.Activity;
import android.os.Bundle;
import android.location.*;
import android.content.*;

public class LocationDemo extends Activity {

 TextView addressText;
 Location currentLocation;
 double currentLatitude;
 double currentLongitude;
 String store;
 @Override

public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.main);
     addressText = (TextView)findViewById(R.id.addressText);
     addressText.setText("ready");
     LocationManager locationManager = 
    (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
    LocationListener locationListener = new LocationListener() {

    public void onLocationChanged(Location location) {
        updateLocation(location);
    }
    public void onStatusChanged(
            String provider, int status, Bundle extras) {}
    public void onProviderEnabled(String provider) {}
    public void onProviderDisabled(String provider) {}
};
locationManager.requestLocationUpdates(
        LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
currentLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 

try{
    List<Address> addresses = new Geocoder(LocationDemo.this,Locale.getDefault()).getFromLocation(currentLocation.getLatitude(), currentLocation.getLongitude(), 1);        
    StringBuilder result = new StringBuilder();
    if (addresses.size() > 0) {      
            Address address =  addresses.get(0);
            int maxIndex = address.getMaxAddressLineIndex();
            for (int x = 0; x <= maxIndex; x++ ){
                result.append(address.getAddressLine(x));
                //result.append(",");
            }                    
        }
         addressText.setText(result.toString()); 
        Intent send_add = new Intent();
        send_add.putExtra("address",result.toString());
         store = addressText.getText().toString(); 
}
catch(IOException ex)
{
addressText.setText(ex.getMessage().toString());
} 
} 
void updateLocation(Location location){
   currentLocation = location;
   currentLatitude = currentLocation.getLatitude();
  currentLongitude = currentLocation.getLongitude();
 } 
}

我在应用程序中导入此代码,该应用程序扩展了服务启动时尝试获取地址的服务

3 个答案:

答案 0 :(得分:0)

LocationListener[] mLocationListeners = new LocationListener[] {
        new LocationListener(LocationManager.GPS_PROVIDER),
        new LocationListener(LocationManager.NETWORK_PROVIDER) };

在您的位置监听器中尝试此代码。

答案 1 :(得分:0)

您可能会将当前位置设为null。因为这需要一些时间来计算位置。如果是这种情况,试试这个

   import java.io.IOException;
    import java.util.*;
    import android.widget.*;
    import android.app.Activity;
    import android.os.Bundle;
    import android.location.*;
    import android.content.*;

    public class LocationDemo extends Activity {

     TextView addressText;
     Location currentLocation;
     double currentLatitude;
     double currentLongitude;
     String store;
     @Override

    public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);
         addressText = (TextView)findViewById(R.id.addressText);
         addressText.setText("ready");
         LocationManager locationManager = 
        (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
        LocationListener locationListener = new LocationListener() {

        public void onLocationChanged(Location location) {
           setAddress(location)
        } 
        public void onStatusChanged(String provider, int status, Bundle extras) {}
        public void onProviderEnabled(String provider) {}
        public void onProviderDisabled(String provider) {}
    };
        locationManager.requestLocationUpdates(
                LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
     }






public void setAddress(Location location){
currentLocation = location;
       currentLatitude = currentLocation.getLatitude();
      currentLongitude = currentLocation.getLongitude();
try{
        List<Address> addresses = new Geocoder(LocationDemo.this,Locale.getDefault()).getFromLocation(currentLocation.getLatitude(), currentLocation.getLongitude(), 1);        
        StringBuilder result = new StringBuilder();
        if (addresses.size() > 0) {      
                Address address =  addresses.get(0);
                int maxIndex = address.getMaxAddressLineIndex();
                for (int x = 0; x <= maxIndex; x++ ){
                    result.append(address.getAddressLine(x));
                    //result.append(",");
                }                    
            }
             addressText.setText(result.toString()); 
            Intent send_add = new Intent();
            send_add.putExtra("address",result.toString());
             store = addressText.getText().toString(); 
    }
    catch(IOException ex)
    {
    addressText.setText(ex.getMessage().toString());
    } 
}
}

答案 2 :(得分:0)

当然你得到一个空指针,因为在调用onLocationChanged之前currentLocation为null。因此,您必须将代码放在那里:

LocationListener locationListener = new LocationListener() {

    public void onLocationChanged(Location location) {

      // put your code here once you have the location

    }
    public void onStatusChanged(
            String provider, int status, Bundle extras) {}
    public void onProviderEnabled(String provider) {}
    public void onProviderDisabled(String provider) {}
};