通常情况下,“无效缓存并重新启动”解决问题,“重建项目”,“重新启动程序”或“重新启动计算机”工作,但它似乎与我的项目或其他东西不同步。
在我的Mainactivity中,我可以毫无问题地使用Editor,但是对于我的其他类(在相同的文件夹中),我不能像以前一样使用相同的编辑器方法。
下面是我的几行代码
SharedPreferences sharedPreferences = this.getSharedPreferences("com.example.carl.gps.prefs", Context.MODE_PRIVATE);
sharedPreferences.Editor = sharedPreferences.edit();
float totalDistanceInMeters = sharedPreferences.getFloat("totalDistanceInMeters", 0f);
boolean firstTimeGettingPosition = sharedPreferences.getBoolean("firstTimeGettingPosition", true);
if (firstTimeGettingPosition) {
editor.putBoolean("firstTimeGettingPosition", false);
} else {
Location previousLocation = new Location("");
previousLocation.setLatitude(sharedPreferences.getFloat("previousLatitude", 0f));
previousLocation.setLongitude(sharedPreferences.getFloat("previousLongitude", 0f));
float distance = location.distanceTo(previousLocation);
totalDistanceInMeters += distance;
editor.putFloat("totalDistanceInMeters", totalDistanceInMeters);
}我遇到的另一个问题是对这一行的许可:
LocationServices.FusedLocationApi.requestLocationUpdates(
googleApiClient, locationRequest, this);我已经声明了如下权限:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>在我看来,这应该足够了,但我对Android Studio还是比较陌生的,所以也许我错了。
这是我的依赖项:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.1.0'
testCompile 'junit:junit:4.12'
compile 'com.google.android.gms:play-services:10.0.1'
compile 'com.loopj.android:android-async-http:1.4.9'}
我将非常感谢任何关于这个错误的帮助。
发布于 2020-06-21 15:28:23
是的,它会抛出错误“无法解析编辑器”,因为你必须写sharedPreferences.Editor,而不是写这个SharedPreferences.Editor editor=sharedPreferences.edit();,希望它能解决你的问题
https://stackoverflow.com/questions/41943802
复制相似问题