为什么我不能使用Resources.getSystem()没有运行时错误?

编程入门 行业动态 更新时间:2024-10-23 05:49:18
本文介绍了为什么我不能使用Resources.getSystem()没有运行时错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 公开类BobDatabase扩展SQLiteOpenHelper { private static final String DATABASE_NAME =bob.db;

private static final int DATABASE_VERSION = 1; public static final String DEFAULT_PROFILE =default_profile; public BobDatabase(Context context) { super(context,DATABASE_NAME,null,DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase数据库) { createProfileTable(database); createTimeTable(database); createEventTable(database); createLocationTable(database); } / ** *为Profile对象创建一个表,执行string.xml中包含的字符串create_profile_table_sql * * @param数据库 * / public void createProfileTable(SQLiteDatabase database) { database.execSQL(Resources.getSystem()。getString(R.string.create_profile_table_sql)); }}

我收到这个错误

/ p> 01-14 12:20:57.591:E / AndroidRuntime(1825) b $ b

导致错误的代码是createProfileTable中的单行,具体如下: Resources.getSystem()。getString(R.string.create_profile_table_sql)如果我使用类变量保存一个上下文并执行 context.getString(R.string.create_profile_table_sql)我没有收到任何错误,但我不想这样做,因为我想避免内存泄漏,根据我知道这应该工作。任何想法发生了什么?

解决方案

根据Android文档, Resources.getSystem()仅提供系统级资源,而不是应用级资源(如strings.xml文件中的资源)。

developer.android/reference/android/content/res/Resources.html #getSystem()

如果您真的想通过这种方式检索您的字符串,或者在您的问题的评论中提出我的建议,请尝试使用应用程序的上下文。

public class BobDatabase extends SQLiteOpenHelper{ private static final String DATABASE_NAME = "bob.db"; private static final int DATABASE_VERSION = 1; public static final String DEFAULT_PROFILE = "default_profile"; public BobDatabase(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase database) { createProfileTable(database); createTimeTable(database); createEventTable(database); createLocationTable(database); } /** * Creates a table for Profile objects, executes the string create_profile_table_sql * contained within strings.xml * @param database */ public void createProfileTable(SQLiteDatabase database) { database.execSQL(Resources.getSystem().getString(R.string.create_profile_table_sql)); }}

I get this error

01-14 12:20:57.591: E/AndroidRuntime(1825): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x7f040003

The code that causes the error is the single line inside createProfileTable specifically, Resources.getSystem().getString(R.string.create_profile_table_sql) if I use a class variable to hold a Context and do context.getString(R.string.create_profile_table_sql) I don't get any errors but I don't want to do that because I want to avoid memory leaks and according to what I know this should work. Any idea what's happening?

解决方案

According to Android documentation, Resources.getSystem() only provides system-level resources, not application-level ones (like the resources inside your strings.xml file).

developer.android/reference/android/content/res/Resources.html#getSystem()

Try using the application's context if you really want to retrieve your strings this way, or take my suggestion in the comment to your question.

更多推荐

为什么我不能使用Resources.getSystem()没有运行时错误?

本文发布于:2023-10-31 04:56:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1545065.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:我不   错误   Resources   getSystem

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!