0
我在Android中顯示ListView中的ArrayList時遇到問題。當嘗試在ListView中加載數組ListList時,我收到空指針異常。其實我試圖在Android列表視圖中加載內存卡內容。(顯示特定文件夾內容)。我是Android開發新手,請幫助。在ListView中顯示ArrayList內容
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();
}
}
});
}
}
上線您正在獲得'NPE'行號和該行的代碼 –
轉到LogCat,然後粘貼堆棧追蹤NullPointerException。 –
得到答案.. !!! –