在LocationManager isrequestLocationUpdates时获取致命异常

时间:2013-06-02 11:54:45

标签: android geolocation location android-location

我在下一行

收到致命异常
  

lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0,ll);

完成错误日志::

06-02 17:07:45.953: D/AndroidRuntime(8179): Shutting down VM
06-02 17:07:45.953: W/dalvikvm(8179): threadid=1: thread exiting with uncaught exception (group=0x40018578)
06-02 17:07:46.031: E/AndroidRuntime(8179): FATAL EXCEPTION: main
06-02 17:07:46.031: E/AndroidRuntime(8179): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.nirbhaya/com.example.nirbhaya.CurrentDetails}: java.lang.SecurityException: Provider network requires ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION permission
06-02 17:07:46.031: E/AndroidRuntime(8179):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
06-02 17:07:46.031: E/AndroidRuntime(8179):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
06-02 17:07:46.031: E/AndroidRuntime(8179):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
06-02 17:07:46.031: E/AndroidRuntime(8179):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
06-02 17:07:46.031: E/AndroidRuntime(8179):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-02 17:07:46.031: E/AndroidRuntime(8179):     at android.os.Looper.loop(Looper.java:130)
06-02 17:07:46.031: E/AndroidRuntime(8179):     at android.app.ActivityThread.main(ActivityThread.java:3687)
06-02 17:07:46.031: E/AndroidRuntime(8179):     at java.lang.reflect.Method.invokeNative(Native Method)
06-02 17:07:46.031: E/AndroidRuntime(8179):     at java.lang.reflect.Method.invoke(Method.java:507)
06-02 17:07:46.031: E/AndroidRuntime(8179):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
06-02 17:07:46.031: E/AndroidRuntime(8179):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
06-02 17:07:46.031: E/AndroidRuntime(8179):     at dalvik.system.NativeStart.main(Native Method)
06-02 17:07:46.031: E/AndroidRuntime(8179): Caused by: java.lang.SecurityException: Provider network requires ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION permission
06-02 17:07:46.031: E/AndroidRuntime(8179):     at android.os.Parcel.readException(Parcel.java:1322)
06-02 17:07:46.031: E/AndroidRuntime(8179):     at android.os.Parcel.readException(Parcel.java:1276)
06-02 17:07:46.031: E/AndroidRuntime(8179):     at android.location.ILocationManager$Stub$Proxy.requestLocationUpdates(ILocationManager.java:646)
06-02 17:07:46.031: E/AndroidRuntime(8179):     at android.location.LocationManager._requestLocationUpdates(LocationManager.java:582)
06-02 17:07:46.031: E/AndroidRuntime(8179):     at android.location.LocationManager.requestLocationUpdates(LocationManager.java:446)
06-02 17:07:46.031: E/AndroidRuntime(8179):     at com.example.nirbhaya.CurrentDetails.onCreate(CurrentDetails.java:65)
06-02 17:07:46.031: E/AndroidRuntime(8179):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-02 17:07:46.031: E/AndroidRuntime(8179):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
06-02 17:07:46.031: E/AndroidRuntime(8179):     ... 11 more

以及 CurrentDetails.java 的完整代码:     包com.example.nirbhaya;

public class CurrentDetails extends Activity implements OnClickListener{

    Button ok = null;
    EditText loc = null,comeby = null,name = null;
    TimePicker setTime;
    String location,Name,comingby,destTime;
    boolean GPS,flag;
    String cityName=null; 
    String subCityName = null;
    String SubThorugh = null;
    String Through = null;
    Intent i;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.currentdetails);

        ok = (Button)findViewById(R.id.bOK);
        loc = (EditText)findViewById(R.id.etPreLoc);
        comeby = (EditText)findViewById(R.id.etComing);
        name = (EditText)findViewById(R.id.etDriver);
        setTime = (TimePicker)findViewById(R.id.timePicker1);

        i = getIntent();
        GPS = i.getBooleanExtra("GPSneed", false);

        ok.setOnClickListener(this);
        if(GPS)
        {
            flag = displayGpsStatus();
            if (flag)
            {
                        LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
                        LocationListener ll = new mylocationlistener();
                        lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, ll);  
            }
            else
            {
                alertbox("Gps Status!!", "Your GPS is: OFF");
            }
        }

        String g = String.valueOf(GPS);
        Toast.makeText(getApplicationContext(), "GPS is: "+g , Toast.LENGTH_LONG).show();
    }
    @Override

private class mylocationlistener implements LocationListener 
{
    @Override 
    public void onLocationChanged(Location location) 
    {    
        if (location != null) 
        {
            Log.d("LOCATION CHANGED", location.getLatitude() + "");
            Log.d("LOCATION CHANGED", location.getLongitude() + "");
            Geocoder gcd = new Geocoder(getBaseContext(), Locale.getDefault());                  
            List<Address>  addresses;  
            try 
            {  
                 addresses = gcd.getFromLocation(location.getLatitude(), location.getLongitude(), 1);  
                 if (addresses.size() > 0)  
                 System.out.println(addresses.get(0).getLocality());  
                 cityName=addresses.get(0).getLocality();  
                 subCityName = addresses.get(0).getSubLocality();
                 SubThorugh = addresses.get(0).getSubThoroughfare();
                 Through = addresses.get(0).getThoroughfare();
            } catch (IOException e) 
            {                 
                e.printStackTrace();  
            } 


        } 
    } 
@Override
public void onProviderDisabled(String provider) {

}    
@Override
public void onProviderEnabled(String provider) {

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

}
}

1 个答案:

答案 0 :(得分:1)

打开AndroidManifest.xml文件并添加所需的权限

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />