我想從數據庫「listOfFolder」表「文件夾」訪問所有的數據,並希望將數據存儲在字符串數組文件夾[]但我得到如何解決ArrayIndexOutOfBoundsException。如何解決ArrayIndexOutOfBoundsException
try{
mydb = openOrCreateDatabase("listOfFolder", MODE_WORLD_WRITEABLE, null);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
int count = 0;
String folders[] = null;
Cursor folderCursor = mydb.query("folder", null, null, null, null, null, null);
while(folderCursor.moveToNext()) {
folders[count] = folderCursor.getString(folderCursor.getColumnIndex("name"));
count++;
}
ListView list = (ListView)findViewById(R.id.lv);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(DropboxActivity.this, android.R.layout.simple_list_item_1,folders);
list.setAdapter(adapter);
setContentView(R.layout.listoffolder);
之後,我想創建一個列表視圖,再次我得到nullpointerexception那裏ListView list =(ListView)findViewById(R.id.lv); \t \t \t ArrayAdapter adapter = new ArrayAdapter (DropboxActivity.this,android.R.layout.simple_list_item_1,folders); \t \t \t list.setAdapter(adapter); \t \t \t setContentView(R.layout.listoffolder); –
sachin
獲得空指針異常在 \t \t \t list.setAdapter(adapter); – sachin
可能'findViewById'找不到您的ListView。你確定在佈局xml文件中有一個ID爲「@ + id/lv」的ListView嗎? – vasart