我正在构建一个简单的文件浏览器,并使用 simple_list_item_1 和 onItemClick(AdapterView父视图,视图视图,int位置,长ID)在 ListView 上显示文件< / strong>此oncItemClick适用于所有其他文件夹点击
但点击最后两项我的应用程序崩溃并收到以下错误
java.lang.ArrayIndexOutOfBoundsException:length = 2;指数= 9
请看看错误的代码
public class UploadActivity extends AppCompatActivity {
private ListView listView;
protected File[] files;
protected LinkedList<String> pathList;
protected String path="/sdcard";
protected final String generalPath="/sdcard";
protected Bundle bundle;
private View emtyFolder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_upload);
pathList=new LinkedList<>();
bundle=getIntent().getExtras();
listView=(ListView)findViewById(R.id.local_files_listView);
emtyFolder=getLayoutInflater().inflate(R.layout.emty_folder,null);
openPath(path);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (files[position].isDirectory())
{
Log.e("Folder Name",files[position].getName());
if(files[position].listFiles().length>0) {
for (int i=0; i<files[position].listFiles().length;i++){
Log.e("File ",files[i].getName());
}
String newpath = files[position].getName();
forwardPath(newpath);
openPath(path);
}
else {
emtyFolder();
String newpath = files[position].getName();
forwardPath(newpath);
listView.setEmptyView(emtyFolder);
}
}
if (files[position].isFile()) {
Log.e("Is a file","True");
String host=bundle.get("host").toString();
String user=bundle.get("user").toString();
String password=bundle.get("password").toString();
int port= Integer.parseInt(bundle.get("port").toString());
String remote=bundle.get("remote").toString();
Transaction transaction=new
Transaction(host,user,password,port,getApplicationContext());
try {
Log.e("Upload","Starting");
boolean status=transaction.Upload(remote, new File(path +
"/" + files[position].getName()));
Log.e("Upload", String.valueOf(status));
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
}
public void backWardPath() {
if (!pathList.isEmpty()) {
this.path = generalPath;
pathList.removeLast();
for (int i = 0; i < pathList.size(); i++) {
this.path = this.path + pathList.get(i);
}
}
openPath(path);
}
public void forwardPath(String newPath) {
this.path = generalPath;
pathList.add("/" + newPath);
for (int i = 0; i < pathList.size(); i++) {
this.path = this.path + pathList.get(i);
}
}
@Override
public void onBackPressed() {
setContentView(R.layout.activity_upload);
Log.e("Path",path);
backWardPath();
}
public void openPath(String path) {
File file = new File(path);
files = file.listFiles();
ArrayAdapter<File> arrayAdapter=new ArrayAdapter<File>(this,android.R.layout.simple_list_item_1,files);
listView.setAdapter(arrayAdapter);
}
private void emtyFolder(){
Toast.makeText(this,"Empty Folder",Toast.LENGTH_LONG).show();
}
}
答案 0 :(得分:0)
编译器告诉您尝试访问超出数组范围的索引。
java.lang.ArrayIndexOutOfBoundsException:length = 2;指数= 9
数组的长度为2,并且您尝试使用索引9进行访问。检查编译器会告诉您问题出现的位置。
答案 1 :(得分:0)
这部分:
for (int i=0; i<files[position].listFiles().length;i++){
Log.e("File ",files[i].getName());
}
看起来非常可疑。 files[position]
指示的目录中的文件数与files
中的文件数完全没有关系。可能你需要另一组File
个对象。