【发布时间】:2018-09-21 17:37:20
【问题描述】:
我正在构建一个简单的文件浏览器并使用 simple_list_item_1 和 onItemClick(AdapterView parent, View view, int position, long id) 在 ListView 上显示文件/strong> 这个 oncItemClick 适用于所有其他文件夹点击
但是在单击最后两项时,我的应用程序崩溃并出现以下错误
java.lang.ArrayIndexOutOfBoundsException:长度=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();
}
}
【问题讨论】:
-
你是对的,我收到了你提到的错误链接但我卡在我的代码中我不知道有什么问题
-
错误的整个 logcat 是什么?
标签: java android file listview android-adapter