我正在尝试列出外部存储中的所有文件,但总是返回null。
if (Environment.getExternalStorageDirectory().exists()) {
Log.i(FILE, "is not empty");
File[] files = Environment.getExternalStorageDirectory().listFiles();
for (File file : files) {
Log.i("File ", file.getAbsolutePath());
}
}
如何列出外部目录中的所有文件
我得到了一个例外
Caused by: java.lang.NullPointerException
at com.example.sam.read.fragments.MyFragment.onCreate(MyFragment.java:138)
at android.support.v4.app.Fragment.performCreate(Fragment.java:1763)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:913)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:548)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1164)
at android.app.Activity.performStart(Activity.java:5114)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2153)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
您在清单中缺少权限。添加
select c.*
from (select c.*,
row_number() over (partition by MAKE&MODEL order by price desc) as seqnum
from cars c
) c
where seqnum = 1;
到您的清单。
答案 1 :(得分:1)
config.assets.compile = true
可能来自NullPointer
,原因是for (File file : files)
null 。
From the documentation,如果指定的路径不是目录,则files
将返回null;它没有告诉你的是,如果你有FileSystem权限问题,你很可能也会得到这个值。
listFiles()
您是否在清单中定义了所需的权限?
if ( Environment.getExternalStorageDirectory().exists() )
如果这不是问题,那么我唯一的想法是<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
null ,或 138 行不在此代码段中。