【问题标题】:Reading files from application memory从应用程序内存中读取文件
【发布时间】:2013-10-04 18:41:55
【问题描述】:

我正在尝试获取一些我保存在我的应用程序中的字符串并打印它们。出于某种原因,我的应用程序崩溃了。你可以帮帮我吗? :)

ListView list;
String[] listC, filenames;
String entry;
int files, i;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.pastbets);

    list = (ListView) findViewById(R.id.list);
    listC = new String[filenames.length/2];
    Prepare();

    ArrayAdapter<String> filenameAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listC);
    list.setAdapter(filenameAdapter);
}

private void Prepare() {
    filenames = getApplicationContext().fileList();
    files = filenames.length/2;
    for(i=0; i<files; i++) {
        entry = openFile(filenames[2*i])+" - "+openFile(filenames[2*i+1]);
        listC[i] = entry;
    }
}

我认为'openFile'功能还可以,不过...

private String openFile(String selectFile) {
    String file = "";
    FileInputStream fis;

    try {
        fis = openFileInput(selectFile);
        byte[] input = new byte[fis.available()];
        while(fis.read(input) != -1){
            file += new String(input);
        }
        fis.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return file;
}

【问题讨论】:

  • 你使用的是filenames,没有设置任何东西。

标签: android string file


【解决方案1】:

应用程序崩溃是因为filenames 数组在使用之前没有初始化,所以在初始化listC 数组之前调用Prepare() 方法:

 list = (ListView) findViewById(R.id.list);
 Prepare();
 listC = new String[filenames.length/2];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    • 2014-03-04
    • 2013-05-21
    • 1970-01-01
    相关资源
    最近更新 更多