I'm facing a problem in showing ArrayList in ListView in android.I'm getting a null pointer exception when try to load array List in ListView. Actually I'm trying to load Memory cards contents in android List View.(Showing Specific Folders Contents).I'm new to Android Development,please help.
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) {
// TODO Auto-generated method stub
if(mExternalMediaAvailable){
Toast.makeText(getApplicationContext(), "External Media..", Toast.LENGTH_LONG).show();
//getDir(Environment.getExternalStorageDirectory().toString());
//Intent i = new Intent(Intent.ACTION_VIEW);
String SD_Card_Path=Environment.getExternalStorageDirectory().toString();
//String SD_Card="/sdcard/reports";
File file = new File(SD_Card_Path);
File[] file_Array=file.listFiles();
Toast.makeText(getApplicationContext(), file_Array.toString(), Toast.LENGTH_LONG).show();
lv=(ListView)findViewById(R.id.list_view);
for(int i=0;i< file_Array.length;i++){
file=file_Array[i];
if(file.isDirectory()){
Log.d("Directory","Added...");
item.add(file.getName()+"/");
}
else{
Log.d("File","Added...");
item.add(file.getName());
}
}
Log.d("adapter", "Before...");
ArrayAdapter<String> adapter=new ArrayAdapter<String>(Access_MemCardActivity_main.this,android.R.layout.simple_list_item_1,item);
lv.setAdapter(adapter);
Log.d("set_adapter", "After...");
Toast.makeText(getApplicationContext(), adapter.toString(),Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(getApplicationContext(), "You Dont Have External Media..", Toast.LENGTH_LONG).show();
}
}
});
}
}