2012-12-03 6 views
0

ListView에서 ArrayList를 표시 할 때 문제가 있습니다. ListView에서 배열 List를로드하려고하면 null 포인터 예외가 발생합니다. 실제로 안드로이드 목록보기에서 메모리 카드 내용을로드하려고합니다. (특정 폴더 내용 표시). 저는 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

ArrayList를크기를 인쇄하여 항목 여부가 포함되어 있는지 여부를 검사를 어댑터을 설정하기 전에 'NPE' 라인 번호와 그 라인의 코드를 얻고 있습니다. –

+0

LogCat로 가서 스택을 붙여 넣으십시오 NullPointerException의 추적. –

+1

Got Answer .. !!! –

답변

1

당신의 의 ListView에 ArrayList를 어떤 줄에 로그인