2012-12-03 118 views
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(); 
     } 
    } 
});   
} 

}

+0

上线您正在获得'NPE'行号和该行的代码 –

+0

转到LogCat,然后粘贴堆栈追踪NullPointerException。 –

+1

得到答案.. !!! –

回答

1

之前适配器设置为您的ListView检查ArrayList中是否包含项目或不是由印刷的ArrayList大小登录