【问题标题】:java.lang.ArrayIndexOutOfBoundsException: length=2; index=9 [duplicate]java.lang.ArrayIndexOutOfBoundsException:长度=2;索引=9 [重复]
【发布时间】:2018-09-21 17:37:20
【问题描述】:

我正在构建一个简单的文件浏览器并使用 simple_list_item_1onItemClick(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();
   }

}

enter image description here

enter image description here

【问题讨论】:

  • 你是对的,我收到了你提到的错误链接但我卡在我的代码中我不知道有什么问题
  • 错误的整个 logcat 是什么?

标签: java android file listview android-adapter


【解决方案1】:

这部分:

for (int i=0; i<files[position].listFiles().length;i++){
    Log.e("File ",files[i].getName());
}

看起来很可疑。 files[position] 指示的目录中的文件数与files 中的文件数之间绝对没有关系。可能您需要另一个 File 对象数组。

【讨论】:

    【解决方案2】:

    编译器告诉您尝试访问超出数组范围的索引。

    java.lang.ArrayIndexOutOfBoundsException: length=2;索引=9

    您的数组的长度为 2,您尝试使用索引 9 进行访问。检查编译器告诉您问题所在的行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-29
      • 1970-01-01
      • 2022-12-16
      • 1970-01-01
      • 2023-04-10
      • 2020-08-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多