我正在为 Android 做一个应用程序,我需要的是它显示 SD 卡中所有文件和目录的列表,并且它必须能够在不同的目录中移动。我在 anddev 中找到了一个很好的教程。我修改了一些东西,使应用程序在 SD 卡中移动,而不是在 Android 根目录中,但其余部分基本相同。
这是我的活动 xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</ListView>
这是活动的代码:
import hackreatorz.cifrador.R;
import java.io.File;
import java.util.ArrayList;
import android.app.ListActivity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class ArchivosSD extends ListActivity {
private ArrayList<String> directoryEntries = new ArrayList<String>();
private File currentDirectory = new File("/sdcard/");
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
browseToSD();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
private void browseToSD() {
browseTo(new File("/sdcard/"));
}
private void upOneLevel() {
if(this.currentDirectory.getParent() != null)
this.browseTo(this.currentDirectory.getParentFile());
}
private void browseTo(final File directory) {
if (directory.isDirectory()) {
this.currentDirectory = directory;
fill(directory.listFiles());
} else {
Toast.makeText(ArchivosSD.this, this.directoryEntries.get(this.getSelectedItemPosition()), Toast.LENGTH_SHORT).show();
}
}
private void fill(File[] files) {
this.directoryEntries.clear();
this.directoryEntries.add(getString(R.string.current_dir));
if(this.currentDirectory.getParent() != null)
this.directoryEntries.add(getString(R.string.up_one_level));
int currentPathStringLength = (int) this.currentDirectory.getAbsoluteFile().length();
for (File file : files) {
this.directoryEntries.add(file.getAbsolutePath().substring(currentPathStringLength));
}
setListAdapter(new ArrayAdapter<String>(this, R.layout.archivos_sd, this.directoryEntries));
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
int selectionRowID = (int) this.getSelectedItemPosition();
String selectedFileString = this.directoryEntries.get(selectionRowID);
if (selectedFileString.equals(".")) {
this.browseToSD();
} else if(selectedFileString.equals("..")) {
this.upOneLevel();
} else {
File clickedFile = null;
clickedFile = new File(this.currentDirectory.getAbsolutePath() + this.directoryEntries.get(selectionRowID));
if(clickedFile != null)
this.browseTo(clickedFile);
}
}
}
我在 Eclipse 中没有收到任何错误,但是在手机上运行应用程序时出现强制关闭,当我查看 Logcat 时,我看到以下内容:
01-01 23:30:29.858: 错误/AndroidRuntime(14911): 致命异常: main 01-01 23:30:29.858: 错误/AndroidRuntime(14911): java.lang.UnsupportedOperationException: addView(View, LayoutParams) 不是在 AdapterView 中支持
我不知道该怎么做,我在谷歌上查了一下,我什么也没找到,我在 stackoverflow 上也做了同样的事情。这是我在 Java 和 Android 中的第一个应用程序,所以我是一个真正的 n00b,如果答案在那里,我不明白,所以如果你能解释我应该做什么来修复这个错误以及为什么,我将不胜感激。
提前感谢您所做的一切。