目前在我的应用程序中,当我使用时,我已经报告了NullPointerExceptions context.getExternalCacheDir(),NullPointerException发生在!cacheDir.exists()中,cacheDir为null,此时抛出NullPointerException;
奇怪的是,以前我检查外部存储器是否已安装。这是代码:
public FileCache(Context context){
if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
cacheDir= context.getExternalCacheDir();
// sometimes return null even the external storage mounted
else
cacheDir=context.getCacheDir();
if(!cacheDir.exists()) // if cacheDir is null throws NullPointerException
cacheDir.mkdirs();
}
请注意,在我的androidManifest.xml中,我有权在外部存储中写入:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>