我正在創建一個應用程序,我在我的位置訪問錯誤在應用程序中的移動。請幫幫我。使用以下代碼......如何以編程方式在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
您是否獲得用戶的許可?新的操作系統6.0可能需要在運行時獲得許可,請提供您的logcat。 – kggoh
這是我的logcat「引發:java.lang.NullPointerException:嘗試調用虛擬方法'android.content.ContentResolver android.content.Context.getContentResolver()'null對象引用 at com.test.MapsActivity.isLocationEnabled( MapsActivity.java:179)「 – Rishabh
似乎還不是一個權限問題。它似乎是在抱怨你的背景。你是否通過了正確的環境?你在哪裏調用這個函數?在活動或片段? OnCreate中的 – kggoh