存储带引号的字符串时 Android SharedPreferences 崩溃
我在共享偏好方面遇到了一些麻烦。我试图在方向更改之前保存搜索框的文本(编辑文本),然后在方向更改后重新创建活动后恢复它。它的工作原理就像一个魅力......除非编辑文本包含引号。当有引号并且我旋转设备时,应用程序在尝试以新方向创建活动时崩溃。只有当有双引号 " 时才会出现这种情况,所有其他符号,包括单引号 ',都可以正常工作。
我得到的错误是 android.database.sqlite.SQLiteException: unrecognized token: "" ORDER BY name COLLATE NOCASE ”。我的猜测是“干扰了SQL查询的编写方式。我尝试在将它们存储在SharedPreferences之前将所有“替换为\”,然后在取出字符串时将所有“替换为”。我仍然得到同样的结果问题。有人知道如何解决这个问题吗?
我的代码如下:
@Override
public void onResume()
{
super.onResume();
String searchText = f_sharedPreferences.getString("searchText", "");
f_searchBox.setText(searchText);
}
@Override
public void onPause()
{
super.onPause();
String searchText = f_searchBox.getText().toString();
f_sharedPreferences.edit().putString("searchText", searchText).commit();
}
非常感谢您的帮助
。 在我的 onCreate 方法中,我通过以下行获取 f_sharedPreferences:
f_sharedPreferences = getSharedPreferences(this.getClass().getName(), Context.MODE_PRIVATE);
另外,我无法使用 android:configChanges="orientation" 因为它会干扰其他功能。
I am having some trouble with shared preferences. I am attempting to save the text of a search box (an edit text) before an orientation change, and then reinstate it after the activity is recreated following the orientation change. It works like a charm.... except when the edit text contains a quotation mark. When there is a quotation mark and I rotate the device, the app crashes when trying to create the activity in the new orientation. This ONLY symbol this happens when there is a double quote ", all other symbols, including the single quote ', work just fine.
The error I get is android.database.sqlite.SQLiteException: unrecognized token: "" ORDER BY name COLLATE NOCASE". My guess is that the " is interfering with the way SQL queries are written. I tried replacing all " with \" before storing them in the SharedPreferences and then replacing all \" with " when getting the string back out. I still get this same problem. Anyone know how to fix this?
My code is as follows:
@Override
public void onResume()
{
super.onResume();
String searchText = f_sharedPreferences.getString("searchText", "");
f_searchBox.setText(searchText);
}
@Override
public void onPause()
{
super.onPause();
String searchText = f_searchBox.getText().toString();
f_sharedPreferences.edit().putString("searchText", searchText).commit();
}
Thanks! Your help is very much appreciated.
EDIT:
In my onCreate method, I get f_sharedPreferences with the following line:
f_sharedPreferences = getSharedPreferences(this.getClass().getName(), Context.MODE_PRIVATE);
Also, I cannot use android:configChanges="orientation" as it interferes with other functionality.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试使用 来自DatabaseUtils的sqlEscapeString(未经测试,只是在谷歌上找到的)
You can try escaping the characters of your string with sqlEscapeString from DatabaseUtils (not tested, just found it on google)
看来是安卓的问题。为什么不将此属性添加到清单的“活动”部分呢?它将保存文本,因此您不必存储它。
请记住,这将禁用 Android 处理旋转的默认方式,因此如果您依赖于使用不同的纵向/横向布局,您可能希望使用另一种方法。
Seems like an Android problem. Why not just add this attribute to the Activity part of your Manifest? It will make the text save so you don't have to store it.
Just keep in mind that this will disable the default way that Android handles rotations, so if you rely on using different layouts for portrait/landscape, you may wish to use another approach.