我正在为Android开发一个应用程序,我需要做的是显示SD卡中所有文件和目录的列表,并且它必须能够在不同目录中移动。我找到了a good tutorial in anddev。我修改了一些内容,以便应用程序在SD卡中移动,而不是在Android根目录中移动,但其余部分基本相同。

这是我的 Activity 的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>

这是Activity的代码:
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时,我得到了一个“强制关闭”信息:



我不知道该怎么办,我已经在Google中查询了内容,却没有找到任何东西,我在stackoverflow上也做了同样的事情。这是我在Java和Android中的第一个应用程序,所以我是一个真正的n00b,如果答案在那儿,我不理解它,因此,如果您能解释该如何解决此错误以及原因,我将不胜感激。

提前感谢您的一切。

最佳答案

当像您一样创建 ArrayAdapter 时,ArrayAdapter需要一个布局资源,其中仅包含TextView。
尝试使用以下方法更改ArrayAdapter的创建:

setListAdapter(new ArrayAdapter<String>(this,
      android.R.layout.simple_list_item_1,
      this.directoryEntries));

看看是否可行。

10-07 19:33
查看更多