2014-10-01 4 views
-1

나는 다음과 같은 목록을 작성했습니다 :목록이 비어 있는지 확인하는 방법?

Geocoder gc = new Geocoder(this); 
List<Address> list = gc.getFromLocationName(location, 1); 
Address add = list.get(0); 

if (add== null) 
{ 
    Toast.makeText(this,"address not found", Toast.LENGTH_SHORT).show(); 
    return; 
} 

주소의 목록을 반환 getFromLocationName 기능을. 주소가없는 경우이 (Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0) 이 발생합니다.

나는 그것을 위해 그 수표를했지만

어떤 생각을 왜 ... 제대로 동작하지 않습니다 내 응용 프로그램은 단지 중지? 목록에 데이터가있는 경우에만 당신이 뭔가를 수행 할 경우 경우

+0

무엇을()? – mosaad

+0

list.isEmpty(); 그것을 확인하십시오 –

+0

괜찮아요 내가 확인 ... –

답변

0
list != null && list.isEmpty() 

, 다음 !list.isEmpty()

 Geocoder gc = new Geocoder(this); 
     List<Address> list = gc.getFromLocationName(location, 1); 
     if(list != null && !list.isEmpty()) { 
      Address add = list.get(0); 
      if (add== null) 
       { 
       Toast.makeText(this,"address not found", Toast.LENGTH_SHORT).show(); 
       return; 
       } 
     } 
3
List<Address> list = gc.getFromLocationName(location, 1); 
if(list==null || list.isEmpty()) 
    return; //list is empty 
Address add = list.get(0); 
+0

정확한 접근 방식으로 일했다. – Hristo

0

이 체크를

if(list.length() != 0) 
0

시도를하려고 사용 이

리스트가 null 또는 사용 목록이 아닌 // 경우
List<Address> list = gc.getFromLocationName(location, 1); 
    if (!list.isEmpty()) { 
     Address add = list.get(0); 
     if (add == null) { 
      Toast.makeText(this, "address not found", Toast.LENGTH_SHORT) 
        .show(); 
      return; 
     } 
    } 
1
if (list.isEmpty()) 
     { 

      Log.i("List is Empty", "Empty List"); 

     } 

! = null의 & & list.isEmpty() list.isEmpty에 대한

관련 문제