public void GetFiles(String Path, String Extension, boolean IsIterative)
{
File[] files = new File(Path).listFiles();
Map<String , String> map ;
for (int i = 0; i < files.length; i++)
{
File f = files[i];
if (f.isFile())
{
if (f.getPath().substring(f.getPath().length() - Extension.length()).equals(Extension))
{
map = new HashMap<String, String>();
map.put("img", f.getPath());
map.put("name", f.getName());
lstPics.add(map);
}
if (!IsIterative)
break;
}
else if (f.isDirectory() && f.getPath().indexOf("/.") == -1)
GetFiles(f.getPath(), Extension, IsIterative);
}
}
为什么它在android 4.4中工作正常,并且在android 6.0中发生了错误? java.lang.NullPointerException:尝试获取null数组的长度 我使用三星note 4运行android 4.4.4 我使用nexus 5运行andoroid 6.0。
10-21 11:55:03.891 23515 23515 E AndroidRuntime: java.lang.NullPointerException: Attempt to get length of null array
10-21 11:55:03.891 23515 23515 E AndroidRuntime: at qjizho.vrlauncher.Launcher.GetFiles(Launcher.java:137)
10-21 11:55:03.891 23515 23515 E AndroidRuntime: at qjizho.vrlauncher.Launcher$1.onItemClick(Launcher.java:58)
10-21 11:55:03.891 23515 23515 E AndroidRuntime: at android.widget.AdapterView.performItemClick(AdapterView.java:310)
10-21 11:55:03.891 23515 23515 E AndroidRuntime: at android.widget.AbsListView.performItemClick(AbsListView.java:1145)
10-21 11:55:03.891 23515 23515 E AndroidRuntime: at android.widget.AbsListView$PerformClick.run(AbsListView.java:3042)
10-21 11:55:03.891 23515 23515 E AndroidRuntime: at android.widget.AbsListView$3.run(AbsListView.java:3879)
10-21 11:55:03.891 23515 23515 E AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:739)
10-21 11:55:03.891 23515 23515 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
10-21 11:55:03.891 23515 23515 E AndroidRuntime: at android.os.Looper.loop(Looper.java:148)
10-21 11:55:03.891 23515 23515 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5417)
10-21 11:55:03.891 23515 23515 E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
10-21 11:55:03.891 23515 23515 E AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
10-21 11:55:03.891 23515 23515 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
答案 0 :(得分:1)
这是因为您没有存储权限,默认情况下不会授予Android M存储空间。
请发布您的目标SDK是什么?你也在AndroidManifest.xml中要求存储权限吗?
public void GetFiles(String Path, String Extension, boolean IsIterative)
{
File[] files = new File(Path).listFiles(); <== This particular line is working fine no issue
Map<String , String> map ;
for (int i = 0; i < files.length; i++) <== this line is giving the Null Pointer Exception because since you don't have storage permission, so files are coming null.
{
File f = files[i];
if (f.isFile())
{
if (f.getPath().substring(f.getPath().length() - Extension.length()).equals(Extension))
{
map = new HashMap<String, String>();
map.put("img", f.getPath());
map.put("name", f.getName());
lstPics.add(map);
}
if (!IsIterative)
break;
}
else if (f.isDirectory() && f.getPath().indexOf("/.") == -1)
GetFiles(f.getPath(), Extension, IsIterative);
}
}