SharedPreferences采坑记
来源:互联网 发布:手把手教开淘宝店 编辑:程序博客网 时间:2024/06/08 07:07
由于对基础的一些知识不是很了解,晕倒在SharedPreferences的石榴裙下。 ~~~
这次就说一下自己所遇到的问题,自己想做一个全局的缓存工具类(就是本应用下所有的类都可以访问的类),在网上百度了下实现方法是这样的。
首先你定义一个自定义类继承Application类。代码如下:
public class GlobalApp extends Application { private static Context mContext; public static Context getContext() { return mContext; } @Override public void onCreate() { super.onCreate(); mContext = this; }}
第二步:编写你自己缓存工具类PrefUtils,代码如下:public class PrefUtils { //自动定位模式 这儿和R.string.automatic_positioning相同 private static final String PRE_AUTOMATIC_POSITIONING = "automatic_positioning"; private static final String PRE_NAME = "com.coolweather.android"; private static SharedPreferences getSharedPreferences1() { return GlobalApp.getContext().getSharedPreferences(PRE_NAME, Context.MODE_PRIVATE); } public static boolean isAutomaticPositioning() { return getSharedPreferences1().getBoolean(PRE_AUTOMATIC_POSITIONING, false); } public static void setAutomaticPositioning(boolean isAutomaticPositioning) { SharedPreferences.Editor editor =getSharedPreferences1().edit(); editor.putBoolean(PRE_AUTOMATIC_POSITIONING, isAutomaticPositioning);}
这样你就可以完成了这个类。
但是、但是、自己在
return GlobalApp.getContext().getSharedPreferences(PRE_NAME, Context.MODE_PRIVATE);
这一行一直报一个空指针错误。找遍了所有的原因了 都没发现错误。最后偶然看见了Application,就百度了一下看到了这
http://www.jianshu.com/p/3138f9c351e8
Application类在项目开发中的使用首先在项目目录下一个Java类继承Application类,实现是onCreate()方法。这个类可以做APP的全局初始化工作,比如图片加载框架的全局配置信息设置。public class AndroidApplication extends Application { private static AndroidApplication instance; @Override public void onCreate() { super.onCreate(); instance = this; } public static AndroidApplication getInstance(){ return instance; }}然后千万不要忘了在Android项目的Manifest文件中指定Application的实现类,不然系统会创建一个默认的Application类。<application android:name=".AndroidApplication" android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> </application>作者:蝉翅的空响链接:http://www.jianshu.com/p/3138f9c351e8來源:简书著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。 editor.commit(); }自己没有在manifest文件下指定Application的实现类,所以就会发生那样的空指针异常,因为系统默认创建了一个默认的Application。
于是自己在manifest文件下准备修改时发现由于自己使用郭霖大神的litepa框架 已经配置了这么一句
android:name="org.litepal.LitePalApplication"
但一个Application只可以配置一个name属性咋办呢?
其实很简单 只需要这样
public class GlobalApp extends LitePalApplication{ ...}
然后在manifest文件下配置上自己的GlobalApp即可。如下:android:name=".GlobalApp"
阅读全文
1 0
- SharedPreferences采坑记
- SharedPreferences
- SharedPreferences
- SharedPreferences
- SharedPreferences
- SharedPreferences
- SharedPreferences
- SharedPreferences
- SharedPreferences
- SharedPreferences
- SharedPreferences
- SharedPreferences
- SharedPreferences
- SharedPreferences
- SharedPreferences
- SharedPreferences
- SharedPreferences
- SharedPreferences
- CSS Resets
- PHP 的 __FILE__ 常量
- Java学习中final关键字用法总结
- Data Warehouse
- linux11
- SharedPreferences采坑记
- 用C++实现希尔排序
- dateutils
- Android调用系统相机,兼容7.0系统
- AIX编译运行问题
- SQL语句
- aop理解
- ConfigReader(四十六)—— ReadShopConfig
- 属性动画