2011-12-21 27 views
0

我寫Antivity基於Android 2.1檢索合同信息:不能獲得基於合同信息在Android 2.1

package com.yarin.android.Examples_03_02; 

import android.app.Activity; 
import android.content.ContentResolver; 
import android.database.Cursor; 
import android.os.Bundle; 
import android.provider.ContactsContract; 
import android.provider.ContactsContract.PhoneLookup; 
import android.widget.TextView; 

public class Activity01 extends Activity 
{ 
    public void onCreate(Bundle savedInstanceState) 
    { 
     TextView tv = new TextView(this); 
     String string = "";  
     super.onCreate(savedInstanceState); 
     //得到ContentResolver對象 
     ContentResolver cr = getContentResolver(); 
     //取得電話本中開始一項的光標 
     Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); 
     //向下移動一下光標 
     while(cursor.moveToNext()) 
     { 
      //取得聯繫人名字 
      int nameFieldColumnIndex = cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME);  
      String contact = cursor.getString(nameFieldColumnIndex); 
      //取得電話號碼 
      int numberFieldColumnIndex = cursor.getColumnIndex(PhoneLookup.NUMBER);  
      String number = cursor.getString(numberFieldColumnIndex); 

      string += (contact+":"+number+"\n"); 
     } 
     cursor.close(); 
     //設置TextView顯示的內容 
     tv.setText(string); 
     //顯示到屏幕 
     setContentView(tv); 
    } 
} 

部署.apk到仿真器和啓動活動後,我得到了以下錯誤:

12-20 12:51:27.183: ERROR/AndroidRuntime(951): FATAL EXCEPTION: main 
     java.lang.RuntimeException: Unable to start activity ComponentInfo{com.yarin.android.Examples_03_02/com.yarin.android.Examples_03_02.Activity01}: java.lang.IllegalStateException: get field slot from row 0 col -1 failed 
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 
     at android.app.ActivityThread.access$2300(ActivityThread.java:125) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 
     at android.os.Handler.dispatchMessage(Handler.java:99) 
     at android.os.Looper.loop(Looper.java:123) 
     at android.app.ActivityThread.main(ActivityThread.java:4627) 
     at java.lang.reflect.Method.invokeNative(Native Method) 
     at java.lang.reflect.Method.invoke(Method.java:521) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
     at dalvik.system.NativeStart.main(Native Method) 
     Caused by: java.lang.IllegalStateException: get field slot from row 0 col -1 failed 
     at android.database.CursorWindow.getString_native(Native Method) 
     at android.database.CursorWindow.getString(CursorWindow.java:329) 
     at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:49) 
     at android.database.CursorWrapper.getString(CursorWrapper.java:135) 
     at com.yarin.android.Examples_03_02.Activity01.onCreate(Activity01.java:30) 
     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 
     ... 11 more 

這迫使應用程序關閉: enter image description here

+0

?目的是什麼? – user370305 2011-12-21 07:48:31

回答

0

檢查你爲什麼使用PhoneLookup.DISPLAY_NAME此代碼

{公共無效setPhoneNumber(意向數據){

 Cursor cursor = managedQuery(data.getData(), null, null, null, null);  
     while (cursor.moveToNext()) 
     {   
      String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); 

      displayName.setText(cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME))); 

      String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)); 

      if (hasPhone.equalsIgnoreCase("1")) 
       hasPhone = "true"; 
      else 
       hasPhone = "false" ; 

      if (Boolean.parseBoolean(hasPhone)) 
      { 
      Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null); 
      while (phones.moveToNext()) 
      { 
      contactNumberEdit.setText(phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))); 
      } 
      phones.close(); 
      } 
     } 
     cursor.close(); 





}} 
+0

你能提供可編譯的代碼片段嗎? thx – 2011-12-21 16:58:53

+0

'ContactsContract.CommonDataKinds.Phone.NUMBER'無法工作。它找不到電話號碼,WTF – 2011-12-21 17:07:05

+0

請確保您在手機或模擬器上添加了一些聯繫人的聯繫人姓名和號碼,請檢查上面的工作代碼 – 2011-12-22 07:19:31

0
{import android.app.Activity; 
import android.content.ContentResolver; 
import android.database.Cursor; 
import android.os.Bundle; 
import android.provider.ContactsContract; 
import android.provider.ContactsContract.PhoneLookup; 
import android.widget.TextView; 

public class DEMOActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     TextView tv = new TextView(this); 
     String string = "";  
     super.onCreate(savedInstanceState); 

     ContentResolver cr = getContentResolver(); 

     Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); 
     String contact=null,hasPhone=null,number=null; 
     while(cursor.moveToNext()) 
     { 
      // To extract the _ID which is the unique field of the contacts table 
      String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); 

      // to extract the name from the contact 
      int nameFieldColumnIndex = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);  
      contact = cursor.getString(nameFieldColumnIndex); 

      // this gives wether the contact has phone number or not 
      hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)); 

      if (hasPhone.equalsIgnoreCase("1")) 
       hasPhone = "true"; 
      else 
       hasPhone = "false" ; 

      if (Boolean.parseBoolean(hasPhone)) 
      { 

      Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null); 
      while (phones.moveToNext()) 
      { 
       // here we extract the contact of give contact ID 
      number=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
      } 
      phones.close(); 
      } 

     string += (contact+":"+number+"\n"); 
     } 
     cursor.close(); 

     tv.setText(string); 

     setContentView(tv); 



    } 
}}