2013-10-04 57 views
0

我試圖獲取一些字符串,這些字符串已保存在我的應用程序中並打印出來。出於某種原因,我的應用程序崩潰。你可以幫幫我嗎? :)從應用程序內存中讀取文件

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; 
} 
+0

您正在使用'filenames'而不將其設置爲任何內容。 – Tyler

回答

0

應用程序崩潰,因爲filenames數組不使用它之前初始化初始化listC以前這麼叫Prepare()方法數組爲:

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

對!謝謝! :) – stavros

相關問題