2012-12-03 36 views
-1

我寫了一個文件選擇器並添加了文件夾和文件ListView。但是在項目上點擊它給我ArrayIndexOutOfBoundsException在我看來的位置。有人能告訴我問題是什麼嗎?Android文件選擇器打開項目點擊

FileUpload.Java

public class FileUpload extends ListActivity { 
    private static final String ITEM_KEY = "key"; 
    Boolean contains=false; 
    private static final String ITEM_IMAGE = "image"; 
    private static String ROOT = "/"; 
    public static final String START_PATH = "START_PATH"; 
    public static final String RESULT_PATH = "RESULT_PATH"; 
    private List<String> item = null; 
    private List<String> path = null; 
    public static final String FORMAT_FILTER = "FORMAT_FILTER"; 
    private TextView myPath; 

    private EditText mFileName; 


    private static String currentpath; 
    private LinkedHashMap<String, Integer> lastPositions = new LinkedHashMap<String, Integer>(); 
    private ArrayList<HashMap<String, Object>> mList; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_file_upload); 
     myPath = (TextView) findViewById(R.id.path); 
     mFileName = (EditText) findViewById (R.id.fdEditTextFile); 
     String startPath = getIntent().getStringExtra(START_PATH); 
     startPath = startPath != null ? startPath : ROOT; 
     getDir(startPath); 
    } 
    public void getDir(String DirPath){ 
     /*Integer position = lastPositions.get(parentPath);*/ 
     if(DirPath!=null){ 
     getDirImpl(DirPath); 
     } 
     } 
    public void getDirImpl(final String dirpath){ 
     currentpath = dirpath; 
     myPath.setText("Location is:"+currentpath); 
     item = new ArrayList<String>(); 
     path = new ArrayList<String>(); 
     mList=new ArrayList<HashMap<String,Object>>(); 
     File f=new File(currentpath); 
     File[] files=f.listFiles(); 


     if (!currentpath.equals(ROOT)) { 

      item.add(ROOT); 
      addItem(ROOT, R.drawable.folder); 
      item.add("../"); 
      addItem("../", R.drawable.folder); 
      path.add(f.getParent()); 
      } 
     TreeMap<String, String> dirsMap = new TreeMap<String, String>(); 
     TreeMap<String, String> dirsPathMap = new TreeMap<String, String>(); 
     TreeMap<String, String> filesMap = new TreeMap<String, String>(); 
     TreeMap<String, String> filesPathMap = new TreeMap<String, String>(); 
     for(int i=0;i<files.length;i++){ 
      File file = files[i]; 

      if(file.isDirectory()){ 
       path.add(file.getPath()); 
       String dirName=file.getName()+"/"; 
       dirsMap.put(dirName, dirName); 
       dirsPathMap.put(dirName, file.getPath()); 

      } 
      else{ 

       final String fileName=file.getName(); 
       filesMap.put(fileName, fileName); 
       filesPathMap.put(fileName, file.getPath()); 
       } 
     } 
     item.addAll(dirsMap.tailMap("").values()); 
     item.addAll(filesMap.tailMap("").values()); 
     item.addAll(dirsPathMap.tailMap("").values()); 

     SimpleAdapter fileList = new SimpleAdapter(this, mList, R.layout.file_dialog_row, new String[] { 
       ITEM_KEY, ITEM_IMAGE }, new int[] { R.id.fdrowtext, R.id.fdrowimage }); 
     for(String dir:dirsMap.tailMap("").values()){ 
      addItem(dir, R.drawable.folder); 
     } 
     for(String file:filesMap.tailMap("").values()){ 
      addItem(file,R.drawable.file); 
     } 
    fileList.notifyDataSetChanged(); 
    setListAdapter(fileList); 
    } 
    public void addItem(String FileName,int imageid){ 
     HashMap<String,Object>item=new HashMap<String, Object>(); 
     item.put(ITEM_KEY, FileName); 
     item.put(ITEM_IMAGE, imageid); 
     mList.add(item); 
    } 

    protected void onListItemClick(ListView l, View v, int position, long id) { 
     File file = new File(path.get(position)); 

     if (file.isDirectory()) { 

      if (file.canRead()) { 
       lastPositions.put(currentpath, position); 
       getDir(path.get(position)); 

      } 
     } else { 
       String filename=file.getName(); 
       mFileName.setText(filename); 
      } 


    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.activity_file_upload, menu); 
     return true; 
    } 

} 

StackRace

12-03 18:08:31.458: E/AndroidRuntime(1784): FATAL EXCEPTION: main 
12-03 18:08:31.458: E/AndroidRuntime(1784): java.lang.IndexOutOfBoundsException: Invalid index 10, size is 0 
12-03 18:08:31.458: E/AndroidRuntime(1784):  at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257) 
12-03 18:08:31.458: E/AndroidRuntime(1784):  at java.util.ArrayList.get(ArrayList.java:311) 
12-03 18:08:31.458: E/AndroidRuntime(1784):  at com.example.projectupload.FileUpload.onListItemClick(FileUpload.java:112) 
12-03 18:08:31.458: E/AndroidRuntime(1784):  at android.app.ListActivity$2.onItemClick(ListActivity.java:321) 
12-03 18:08:31.458: E/AndroidRuntime(1784):  at android.widget.AdapterView.performItemClick(AdapterView.java:284) 
12-03 18:08:31.458: E/AndroidRuntime(1784):  at android.widget.ListView.performItemClick(ListView.java:3382) 
12-03 18:08:31.458: E/AndroidRuntime(1784):  at android.widget.AbsListView$PerformClick.run(AbsListView.java:1696) 
12-03 18:08:31.458: E/AndroidRuntime(1784):  at android.os.Handler.handleCallback(Handler.java:587) 
12-03 18:08:31.458: E/AndroidRuntime(1784):  at android.os.Handler.dispatchMessage(Handler.java:92) 
12-03 18:08:31.458: E/AndroidRuntime(1784):  at android.os.Looper.loop(Looper.java:123) 
12-03 18:08:31.458: E/AndroidRuntime(1784):  at android.app.ActivityThread.main(ActivityThread.java:4627) 
12-03 18:08:31.458: E/AndroidRuntime(1784):  at java.lang.reflect.Method.invokeNative(Native Method) 
12-03 18:08:31.458: E/AndroidRuntime(1784):  at java.lang.reflect.Method.invoke(Method.java:521) 
12-03 18:08:31.458: E/AndroidRuntime(1784):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
12-03 18:08:31.458: E/AndroidRuntime(1784):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
12-03 18:08:31.458: E/AndroidRuntime(1784):  at dalvik.system.NativeStart.main(Native Method) 
+1

請發佈整個堆棧跟蹤 – blacharnia

回答

0

您需要在onListItemClick方法從你的數據結構mList檢索文件名。

+0

仍然我得到了同樣的錯誤,但mList是散列圖 –

+0

問題是,您用來顯示列表(mList)的集合與數組列表(路徑)不同步。檢查你填充路徑數據結構的方式。我認爲你必須在addItem函數調用中添加集合的路徑。 – scout

+0

問題在位置給一些其他文件夾參考 –

相關問題