【问题标题】:Resources.getSystem().openRawResource(random.txt) returns Resource$NotFoundExceptionResources.getSystem().openRawResource(random.txt) 返回 Resource$NotFoundException
【发布时间】:2011-03-23 13:34:57
【问题描述】:

基于 1.6 SDK4 构建

InputStream is = Resources.getSystem().openRawResource(R.raw.vortexrules);

这条线给我带来了无穷无尽的麻烦......

我的 R.java 显示:

public static final class raw {
    public static final int vortexrules=0x7f040000;

这告诉我编译器正在识别文本文件和文件夹并将它们设置为资源。

当我运行上面的代码时,尝试将 InputStream 设置为我得到的文本文件:

07-31 01:53:58.839:错误/AndroidRuntime(19573):未捕获的处理程序:线程主因未捕获的异常而退出 07-31 01:53:58.947: 错误/AndroidRuntime(19573): java.lang.RuntimeException: 无法启动活动 ComponentInfo{com.vortex.rules/com.vortex.rules.MainWindow}: android.content.res.Resources $NotFoundException:资源 ID #0x7f040000 07-31 01:53:58.947: 错误/AndroidRuntime(19573): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2444) 07-31 01:53:58.947: 错误/AndroidRuntime(19573): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2460) 07-31 01:53:58.947: 错误/AndroidRuntime(19573): 在 android.app.ActivityThread.access$2300(ActivityThread.java:119) 07-31 01:53:58.947: 错误/AndroidRuntime(19573): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1837) 07-31 01:53:58.947: 错误/AndroidRuntime(19573): 在 android.os.Handler.dispatchMessage(Handler.java:99) 07-31 01:53:58.947: 错误/AndroidRuntime(19573): 在 android.os.Looper.loop(Looper.java:123) 07-31 01:53:58.947: 错误/AndroidRuntime(19573): 在 android.app.ActivityThread.main(ActivityThread.java:4246) 07-31 01:53:58.947: 错误/AndroidRuntime(19573): 在 java.lang.reflect.Method.invokeNative(Native Method) 07-31 01:53:58.947: 错误/AndroidRuntime(19573): 在 java.lang.reflect.Method.invoke(Method.java:521) 07-31 01:53:58.947: 错误/AndroidRuntime(19573): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791) 07-31 01:53:58.947: 错误/AndroidRuntime(19573): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549) 07-31 01:53:58.947:错误/AndroidRuntime(19573):在 dalvik.system.NativeStart.main(本机方法) 07-31 01:53:58.947: 错误/AndroidRuntime(19573): 由: android.content.res.Resources$NotFoundException: 资源 ID #0x7f040000 07-31 01:53:58.947: 错误/AndroidRuntime(19573): 在 android.content.res.Resources.getValue(Resources.java:888) 07-31 01:53:58.947: 错误/AndroidRuntime(19573): 在 android.content.res.Resources.openRawResource(Resources.java:813) 07-31 01:53:58.947: 错误/AndroidRuntime(19573): 在 android.content.res.Resources.openRawResource(Resources.java:795) 07-31 01:53:58.947: 错误/AndroidRuntime(19573): 在 com.vortex.rules.CSV_Reader.onCreate(CSV_Reader.java:18) 07-31 01:53:58.947: 错误/AndroidRuntime(19573): 在 com.vortex.rules.MainWindow.CreateDatabase(MainWindow.java:103) 07-31 01:53:58.947: 错误/AndroidRuntime(19573): 在 com.vortex.rules.MainWindow.onCreate(MainWindow.java:39) 07-31 01:53:58.947: 错误/AndroidRuntime(19573): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123) 07-31 01:53:58.947: 错误/AndroidRuntime(19573): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2407) 07-31 01:53:58.947: 错误/AndroidRuntime(19573): ... 11 更多

退出 LogCat。

这告诉我它无法找到具有该 ID 的资源,但它只是从 R.java 中获取了 ID,而 R.java 正在从编译器获取其信息。

此时我完全迷失了。

任何帮助将不胜感激......

【问题讨论】:

  • 如果你在标题中写它时调用它 (Resources.getSystem().openRawResource(random.txt)) 那么你会得到一个错误,因为你不应该在加载时包含文件扩展名一种资源,所以它只是“随机的”
  • 抱歉回复晚了,是的,我发现了,但是谢谢回复!
  • 现在你应该接受你自己的答案了。

标签: java android resources inputstream


【解决方案1】:

正如隐形直升机在 cmets 中所说,您不需要文件扩展名!

【讨论】:

    猜你喜欢
    • 2013-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-29
    相关资源
    最近更新 更多