2016-08-03 67 views
0

我正在創建一個應用程序,我在我的位置訪問錯誤在應用程序中的移動。請幫幫我。使用以下代碼......如何以編程方式在android位置訪問

public static boolean isLocationEnabled(Context context) { 
    int locationMode = 0; 
    String locationProviders; 

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){ 
     try { 
      locationMode = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE); 

     } catch (Settings.SettingNotFoundException e) { 
      e.printStackTrace(); 
     } 

     return locationMode != Settings.Secure.LOCATION_MODE_OFF; 

    }else{ 
     locationProviders = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); 
     return !TextUtils.isEmpty(locationProviders); 
    } 


} 

我在這個

「locationMode = Settings.Secure.getInt(context.getContentResolver(),Settings.Secure.LOCATION_MODE)得到錯誤; 「

請告訴我如何解決它。 感謝 click here what i want

+0

您是否獲得用戶的許可?新的操作系統6.0可能需要在運行時獲得許可,請提供您的logcat。 – kggoh

+0

這是我的logcat「引發:java.lang.NullPointerException:嘗試調用虛擬方法'android.content.ContentResolver android.content.Context.getContentResolver()'null對象引用 at com.test.MapsActivity.isLocationEnabled( MapsActivity.java:179)「 – Rishabh

+0

似乎還不是一個權限問題。它似乎是在抱怨你的背景。你是否通過了正確的環境?你在哪裏調用這個函數?在活動或片段? OnCreate中的 – kggoh

回答

0

enter image description here

我想你的代碼,我能夠得到的位置。然後你最好檢查你的上下文是否正確。需要權限?

+0

我想點擊「允許按鈕」位置訪問時自動打開 – Rishabh

+0

所以考慮解決? – kggoh

+0

不,當我打開我的應用程序,我的應用程序沒有獲得位置ON.so,那時我點擊「允許按鈕」的位置訪問自動開啓 – Rishabh

相關問題