NullPoInterException:尝试在Android Studio中访问资产中的文件夹时尝试获得空数组的长度
在我的 android 项目中,我在资产中添加了一个名为 validator 的文件夹。 validator 文件夹内有一些 json 文件。 我想访问它们,然后使用管理这些文件
File[] files = f.listFiles
当我运行应用程序时,出现以下错误:
java.lang.NullPointerException:尝试获取空数组的长度 引起原因:java.lang.NullPointerException:尝试获取长度 空数组
我假设这里的问题是我提供的路径。项目结构如下:
MyApplication
│
└───app
│
└───src
│
└───main
│
└───res
│
└───assets
│
└───java
│
└───Main activity
因此,在主活动中,我创建了一个包含字符串的路径,然后
File f = new File(path);
File[] files = f.listFiles
但结果是一个错误,指出数组为空。 我提供的路径是:
String path = "..\\MyApplication\\app\\src\\main\\assets";
最后,我在清单中添加了有关外部存储的权限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
路径是否不正确或者是否存在其他问题? 先感谢您!
in my android project I have added a folder inside assets called validator. Inside validator folder there are some json files.
I want to access them and then manage these files with
File[] files = f.listFiles
When I am running the application I get the following error:
java.lang.NullPointerException: Attempt to get length of null array
Caused by: java.lang.NullPointerException: Attempt to get length of
null array
I am assuming that the issue here is the path I am providing. The project structure is the following:
MyApplication
│
└───app
│
└───src
│
└───main
│
└───res
│
└───assets
│
└───java
│
└───Main activity
So, inside main activity I have created a path containing the string and then
File f = new File(path);
File[] files = f.listFiles
But the result is an error saying that the array is null.
The path I have provided is :
String path = "..\\MyApplication\\app\\src\\main\\assets";
Finally, I have added the permission in the manifest regarding the external storage
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Is the path incorrect or is there another issue?
Thank you in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论