以下代码无法正常运行,应用程序在启动时因“不幸......”错误而关闭。
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scan_media_store_list_view);
vedio_list = (ListView) findViewById(R.id.vedio_list);
Uri contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
String[] projection = { MediaStore.Video.VideoColumns.DISPLAY_NAME};
cursor = getContentResolver().query(contentUri, projection, null, null, null);
title_index = cursor.getColumnIndexOrThrow(MediaStore.Video.VideoColumns.TITLE);
al = new ArrayList<String>();
showList();
}
private void showList() {
while(cursor.moveToNext())
{
String title = cursor.getString(title_index);
al.add(0 , title);
aa.notifyDataSetChanged();
}
cursor.close();
}
我做错了什么?
答案 0 :(得分:0)
通过获取计数并移动到列表中的第一个来检查光标是否良好:
private void showList() {
if(cursor.getCount() > 0){
cursor.moveToFirst();
while(cursor.moveToNext())
{
String title = cursor.getString(title_index);
al.add(0 , title);
aa.notifyDataSetChanged();
}
}
cursor.close();
}