我正在寻找在按钮单击事件上打开指定的文件夹内容(存在于外部存储卡上)。并将它们显示在单独的列表中。当我加载列表中的内容时,我收到错误为空指针异常。我的源代码是:
public class Access_MemCardActivity_main extends Activity {
Button btn_view;
boolean mExternalMediaAvailable=false;
ArrayList<String> item=new ArrayList<String>();
ArrayAdapter<String> adapter;
private ListView lv;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String state=Environment.getExternalStorageState();
if(Environment.MEDIA_MOUNTED.equals(state)){
mExternalMediaAvailable=true;
}
else{
mExternalMediaAvailable=false;
}
btn_view=(Button)findViewById(R.id.btn_view);
btn_view.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(mExternalMediaAvailable){
Toast.makeText(getApplicationContext(), "External Media..", Toast.LENGTH_LONG).show();
String SD_Card_Path=Environment.getExternalStorageDirectory().toString()+"/reports";
File file = new File(SD_Card_Path);
File[] file_Array=file.listFiles();
Toast.makeText(getApplicationContext(), file_Array.toString(), Toast.LENGTH_LONG).show();
for(int i=0;i< file_Array.length;i++){
file=file_Array[i];
if(file.isDirectory()){
item.add(file.getName()+"/");
}
else{
item.add(file.getName());
}
}
Toast.makeText(getApplicationContext(), item.toString(),Toast.LENGTH_LONG).show();
lv=(ListView)findViewById(R.id.list_view);
adapter=new ArrayAdapter<String>(Access_MemCardActivity_main.this,R.layout.main,R.id.list_view,item);
lv.setAdapter(adapter);
}
else{
Toast.makeText(getApplicationContext(), "You Dont Have External Media..", Toast.LENGTH_LONG).show();
}
}