2011-11-14 2 views
0

아래 오류가 발생합니다.담당자 이름 전화 번호

java.lang.NullPointerException 
11-14 16:46:59.093: E/AndroidRuntime(4064):  at android.content.ContextWrapper.getContentResolver(ContextWrapper.java:90) 

아래의 코드를 사용하십시오.

public String getContactNameFromNumber(String number) { 
     // define the columns I want the query to return 
     String[] projection = new String[] { 
       Contacts.Phones.DISPLAY_NAME, 
       Contacts.Phones.NUMBER }; 


     // encode the phone number and build the filter URI 
     Uri contactUri = Uri.withAppendedPath(Contacts.Phones.CONTENT_FILTER_URL, Uri.encode(number)); 

     // query time 
     Cursor c = getContentResolver().query(contactUri, projection, null, 
       null, null); 

     // if the query returns 1 or more results 
     // return the first result 
     if (c.moveToFirst()) { 
      String name = c.getString(c 
        .getColumnIndex(Contacts.Phones.DISPLAY_NAME)); 
      return name; 
     } 

이 오류가 발생하는 이유는 무엇입니까?

감사합니다.

+0

시도; – Hiral

답변

2

당신은이 코드를 시도 할 수 있습니다 : context.getContentResolver.query (...)를 사용하여

String address="3791783465"; //phone number you already have 

Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(address));   
Cursor cs= context.getContentResolver().query(uri, new String[]{PhoneLookup.DISPLAY_NAME},null,null,null); 

if(cs.getCount()>0) 
{ 
    cs.moveToFirst();  
    Toast.makeText(context,cs.getString(cs.getColumnIndex(PhoneLookup.DISPLAY_NAME),Toast.LENGTH_SHORT).show(); 
    cs.close(); 
} 
else 
    Toast.makeText(context,"Unknown",Toast.LENGTH_SHORT).show(); 
+0

완벽한 답변 ... 좋습니다. – Nirav

+0

도움이되어 기쁩니다 .. :) – Hiral