gpt4 book ai didi

android - NullPointerException - 尝试获取空数组的长度 (readDirectory())

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:57:11 24 4
gpt4 key购买 nike

我有这里显示的代码。我的问题是 files.length 处的 NullPointerException

for(int i=0; i < files.length; i++){

这是因为我有一个“fail readDirectory() errno=13”在

File[] files = f.listFiles();

但为什么在路径良好时我的 readDirectory 会失败?

package com.example.androidexplorer;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.os.Environment;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends ListActivity {

private List<String> item = null;
private List<String> path = null;
private String root;
private TextView myPath;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myPath = (TextView)findViewById(R.id.path);

root = Environment.getExternalStorageDirectory().getPath();
getDir(root);
}

private void getDir(String dirPath)
{
myPath.setText("Location: " + dirPath);
item = new ArrayList<String>();
path = new ArrayList<String>();
File f = new File(dirPath);

Log.v("Path: ", dirPath);

Log.v("BEFORE", "Before Reading Fail...");
File[] files = f.listFiles();
Log.v("AFTER", "After Reading Fail...");


if(!dirPath.equals(root))
{
item.add(root);
path.add(root);
item.add("../");
path.add(f.getParent());
}

Log.v("CRASH", "1 Line before crash");
for(int i=0; i < files.length; i++){
Log.v("AFTER CRASH", "1 Line after crash");
File file = files[i];

if(!file.isHidden() && file.canRead()){
path.add(file.getPath());
if(file.isDirectory()){
item.add(file.getName() + "/");
}else{
item.add(file.getName());
}
}
}

ArrayAdapter<String> fileList =
new ArrayAdapter<String>(this, R.layout.row, item);
setListAdapter(fileList);
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
File file = new File(path.get(position));

if (file.isDirectory())
{
if(file.canRead()){
getDir(path.get(position));
}else{
new AlertDialog.Builder(this)
.setIcon(R.drawable.ic_launcher)
.setTitle("[" + file.getName() + "] folder can't be read!")
.setPositiveButton("OK", null).show();
}
}else {
new AlertDialog.Builder(this)
.setIcon(R.drawable.ic_launcher)
.setTitle("[" + file.getName() + "]")
.setPositiveButton("OK", null).show();

}
}

}

日志:

> 04-23 15:35:34.084: D/ResourcesManager(20672): creating new
> AssetManager and set to
> /data/app/com.example.androidexplorer-1/base.apk 04-23 15:35:34.104:
> I/art(20672): Created application space
> /data/dalvik-cache/arm/data@app@com.example.androidexplorer-1@base.apk@classes.dex.art
> at 0x76eb0000~0x76f15ff8 04-23 15:35:34.104: I/art(20672): Loaded art
> file:
> /data/dalvik-cache/arm/data@app@com.example.androidexplorer-1@base.apk@classes.dex.art
> 04-23 15:35:34.194: V/BitmapFactory(20672):
> DecodeImagePath(decodeResourceStream3) :
> res/drawable-xxhdpi-v4/ic_ab_back_holo_dark_am.png 04-23 15:35:34.204:
> V/BitmapFactory(20672): DecodeImagePath(decodeResourceStream3) :
> res/drawable-xxhdpi-v4/sym_def_app_icon.png 04-23 15:35:34.234:
> D/AbsListView(20672): Get MotionRecognitionManager 04-23 15:35:34.244:
> V/BitmapFactory(20672): DecodeImagePath(decodeResourceStream3) :
> res/drawable-xhdpi-v4/ic_launcher.png 04-23 15:35:34.254:
> V/Path:(20672): /storage/emulated/0 04-23 15:35:34.254:
> V/BEFORE(20672): Before Reading Fail... 04-23 15:35:34.254:
> E/File(20672): fail readDirectory() errno=13 04-23 15:35:34.254:
> V/AFTER(20672): After Reading Fail... 04-23 15:35:34.254:
> V/CRASH(20672): 1 Line before crash 04-23 15:35:34.254:
> D/AndroidRuntime(20672): Shutting down VM 04-23 15:35:34.254:
> E/AndroidRuntime(20672): FATAL EXCEPTION: main 04-23 15:35:34.254:
> E/AndroidRuntime(20672): Process: com.example.androidexplorer, PID:
> 20672 04-23 15:35:34.254: E/AndroidRuntime(20672):
> java.lang.RuntimeException: Unable to start activity
> ComponentInfo{com.example.androidexplorer/com.example.androidexplorer.MainActivity}:
> java.lang.NullPointerException: Attempt to get length of null array
> 04-23 15:35:34.254: E/AndroidRuntime(20672): at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2658)
> 04-23 15:35:34.254: E/AndroidRuntime(20672): at
> android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2725)
> 04-23 15:35:34.254: E/AndroidRuntime(20672): at
> android.app.ActivityThread.access$900(ActivityThread.java:172) 04-23
> 15:35:34.254: E/AndroidRuntime(20672): at
> android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422)
> 04-23 15:35:34.254: E/AndroidRuntime(20672): at
> android.os.Handler.dispatchMessage(Handler.java:102) 04-23
> 15:35:34.254: E/AndroidRuntime(20672): at
> android.os.Looper.loop(Looper.java:145) 04-23 15:35:34.254:
> E/AndroidRuntime(20672): at
> android.app.ActivityThread.main(ActivityThread.java:5834) 04-23
> 15:35:34.254: E/AndroidRuntime(20672): at
> java.lang.reflect.Method.invoke(Native Method) 04-23 15:35:34.254:
> E/AndroidRuntime(20672): at
> java.lang.reflect.Method.invoke(Method.java:372) 04-23 15:35:34.254:
> E/AndroidRuntime(20672): at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388)
> 04-23 15:35:34.254: E/AndroidRuntime(20672): at
> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183) 04-23
> 15:35:34.254: E/AndroidRuntime(20672): Caused by:
> java.lang.NullPointerException: Attempt to get length of null array
> 04-23 15:35:34.254: E/AndroidRuntime(20672): at
> com.example.androidexplorer.MainActivity.getDir(MainActivity.java:57)
> 04-23 15:35:34.254: E/AndroidRuntime(20672): at
> com.example.androidexplorer.MainActivity.onCreate(MainActivity.java:31)
> 04-23 15:35:34.254: E/AndroidRuntime(20672): at
> android.app.Activity.performCreate(Activity.java:6221) 04-23
> 15:35:34.254: E/AndroidRuntime(20672): at
> android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
> 04-23 15:35:34.254: E/AndroidRuntime(20672): at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2611)
> 04-23 15:35:34.254: E/AndroidRuntime(20672): ... 10 more

更新

我的 list 中有权限错误。我一开始有什么:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.spicysoftware.infoid"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="15" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:name="android.permission.READ_EXTERNAL_STORAGE">
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".sysinfo"></activity>
<activity android:name=".daten"></activity>
<activity android:name=".storage"></activity>
<activity android:name="org.achartengine.GraphicalActivity"> </activity>
</application>

</manifest>

并更新:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.spicysoftware.infoid"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="15" />

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">

<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".sysinfo"></activity>
<activity android:name=".daten"></activity>
<activity android:name=".storage"></activity>
<activity android:name="org.achartengine.GraphicalActivity"> </activity>
</application>

</manifest>

最佳答案

f.listFiles()

如果路径不存在则返回null

请检查您的路径。

更新

Android 还需要权限才能读取某些文件。也许您需要将此添加到您的 list 中:

 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

更多信息:

http://developer.android.com/reference/android/Manifest.permission.html

关于android - NullPointerException - 尝试获取空数组的长度 (readDirectory()),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29825198/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com