Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/195.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 获取共享首选项存储上的异常_Android_Android Layout_Android Intent_Android Widget - Fatal编程技术网

Android 获取共享首选项存储上的异常

Android 获取共享首选项存储上的异常,android,android-layout,android-intent,android-widget,Android,Android Layout,Android Intent,Android Widget,我正在尝试使用此选项保存共享首选项 SharedPreferences sharedPref = getSharedPreferences("BluefreeSharedPreferences",0); PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); prefEditor = sharedPref .edit(); prefEditor.putString("UserName", userName)

我正在尝试使用此选项保存共享首选项

SharedPreferences sharedPref = getSharedPreferences("BluefreeSharedPreferences",0); 
PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
prefEditor = sharedPref .edit();
prefEditor.putString("UserName", userName);
prefEditor.putString("Password", password);
prefEditor.commit();
在具有2.2.1版本的三星Galaxy 1900上,它不保存共享首选项,但在具有2.2.1版本的Emulator和具有4.0.3操作系统的HTC Y one上,它工作正常。
我读了一些帖子,这里有人能给我指点指点吗?

我认为你没有正确初始化编辑器,这是我不久前写的一段代码,它优先使用计数器并在执行时递增

SharedPreferences counter = getSharedPreferences("prefs", Context.MODE_PRIVATE);
SharedPreferences.Editor count_editor = counter.edit();
int currentCount = counter.getInt(packageName +"", 0);
currentCount++;
count_editor.putInt("times clicked", currentCount);
count_editor.commit();
试试这个

您好,谢谢您的回答。但是我的代码在HTC和三星4.0.2操作系统上运行良好。它只在三星galaxy 1900上面临问题。我们的代码都在galaxy s2上运行,但是当我的设备连接并运行模拟器时,直到我关闭模拟器,logcat才为我的设备打印。您好,谢谢您的指导和帮助。我正在使用三星Galaxy S 1900和2.2.1操作系统。它不是S2。