2012-03-10 354 views
1

我使用此代碼讓每一個接觸 的PHONENUMBER但我得到以下錯誤:的Android越來越PHONENUMBER

03-10 19:52:44.925:E/AndroidRuntime(15954):致命異常:main 03-10 19:52:44.925:E/AndroidRuntime(15954): java.lang.RuntimeException:無法啓動活動 ComponentInfo {contact.backup.alexander.fuchs/contact.backup.alexander.fuchs.App }: android.database.CursorIndexOutOfBoundsException:索引-1請求, 大小爲1 03-10 19:52:44.925:E/AndroidRuntime(15954):在 android.app .ActivityThread.performLaunchActivity(ActivityThread.java:1651) 03-10 19:52:44.925:E/AndroidRuntime(15954):at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667) 03-10 19: 52:44.925:E/AndroidRuntime(15954):at android.app.ActivityThread.access $ 1500(ActivityThread.java:117)03-10 19:52:44.925:E/AndroidRuntime(15954):at android.app .ActivityThread $ H.handleMessage(ActivityThread.java:935) 03-10 19:52:44.925:E/AndroidRuntime(15954):at android.os.Handler.dispatchMessage(Handler.java:99)03-10 19:52:44.925:E/AndroidRuntime(15954):在 android.os.Looper.loop(Looper.java:130)03-10 19:52:44.925: E/AndroidRuntime(15954):a t android.app.ActivityThread.main(ActivityThread.java:3691)03-10 19:52:44.925:E/AndroidRuntime(15954):at java.lang.reflect.Method.invokeNative(Native Method)03- 10 E:/ AndroidRuntime(15954):在 java.lang.reflect.Method.invoke(Method.java:507)03-10 19:52:44.925: E/AndroidRuntime(15954): at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:907) 03-10 19:52:44.925:E/AndroidRuntime(15954):at com.android.internal.os.ZygoteInit .main(ZygoteInit.java:665)03-10 19:52:44.925:E/AndroidRuntime(15954):at dalvik.system.NativeStart.main(Native Method)03-10 19:52:44.925: E /穿心蓮內酯idRuntime(15954):造成者: android.database.CursorIndexOutOfBoundsException:索引-1請求, 大小爲1 03-10 19:52:44.925:E/AndroidRuntime(15954):at android.database.AbstractCursor。 checkPosition(AbstractCursor.java:580) 03-10 19:52:44.925:E/AndroidRuntime(15954):at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214) 03-10 19:52:44.925 :E/AndroidRuntime(15954):at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41) 03-10 19:52:44.925:E/AndroidRuntime(15954):at android.database.CursorWrapper.getString (CursorWrapper.java:135)03-10 19:52:44.925:E/AndroidRuntime(15954):在 contact.backup.alexander.fuchs.App.onCreate(App.java:85)03-10 19:52:44.925:E/AndroidRuntime(15954):at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java: 1047) 03-10 19:52:44.925:E/AndroidRuntime(15954):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615) 03-10 19:52:44.925:E/AndroidRuntime(15954) ):...11多

我的代碼:

package contact.backup.alexander.fuchs; 

import java.io.BufferedWriter; 
import java.io.File; 
import java.io.FileWriter; 

import android.app.Activity; 
import android.content.ContentResolver; 
import android.content.ContentUris; 
import android.database.Cursor; 
import android.net.Uri; 
import android.os.Bundle; 
import android.provider.Contacts.People; 
import android.provider.Contacts.Phones; 
import android.provider.ContactsContract; 

public class App extends Activity { 
    /** Called when the activity is first created. */ 

    int i; 
    int i2; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 


     util.deleteDir(new File("/data/data/contact.backup.alexander.fuchs/backup/")); 
     new File("/data/data/contact.backup.alexander.fuchs/backup/").mkdirs(); 



     // get it 
     ContentResolver cr = getContentResolver(); 
     Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, 
       null, null, null, null); 


     if (cur.getCount() > 0) { 
      i = 0; 
      i2 = 0; 
     while (cur.moveToNext()) { 
      String id = cur.getString(
         cur.getColumnIndex(ContactsContract.Contacts._ID)); 
     String name = cur.getString(
         cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 


     // write 
     try 
     { 
      new File("/data/data/contact.backup.alexander.fuchs/backup/"+String.valueOf(i)).mkdirs(); 

      FileWriter fstream = new FileWriter("/data/data/contact.backup.alexander.fuchs/backup/"+String.valueOf(i)+"/id.txt"); 
      BufferedWriter out = new BufferedWriter(fstream); 
      out.write(id); 
      //Close the output stream 
      out.close(); 


      fstream = new FileWriter("/data/data/contact.backup.alexander.fuchs/backup/"+String.valueOf(i)+"/name.txt"); 
      out = new BufferedWriter(fstream); 
      out.write(name); 
      //Close the output stream 
      out.close(); 





     } 
     catch (Exception x) 
     { 

     } 
     if (Integer.parseInt(cur.getString(
       cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) { 
      Cursor pCur = cr.query(
      ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
      null, 
      ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", 
      new String[]{id}, null); 

       String number = pCur.getString(
         pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
       while (pCur.moveToNext()) { 
        try 
        { 
        new File("/data/data/contact.backup.alexander.fuchs/backup/"+String.valueOf(id)).mkdirs(); 

        FileWriter fstream = new FileWriter("/data/data/contact.backup.alexander.fuchs/backup/"+String.valueOf(id)+"/number.txt"); 
        BufferedWriter out = new BufferedWriter(fstream); 
        out.write(number); 
        //Close the output stream 
        out.close(); 


        } 
        catch(Exception x) 
        { 

        } 
        } 


      pCur.close(); 

     } 
     i++; 
     } 


    } 

    } 
} 

回答

0
private ArrayList<Map<String, String>> peopleList; 
private AutoCompleteTextView txtPhoneName; 
    private SimpleAdapter mAdapter; 

//在OnCreate中

txtPhoneName=(AutoCompleteTextView)findViewById(R.id.autoDeni); 

peopleList = new ArrayList<Map<String, String>>(); 
PopulatePeopleList(); 


mAdapter = new SimpleAdapter(this, peopleList, R.layout.row ,new String[] { "Name", "Phone" }, new int[] { R.id.text1, R.id.text2 }); 

txtPhoneName.setAdapter(mAdapter); 

public void PopulatePeopleList() 
    { 

peopleList.clear(); 

Cursor people = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); 

while (people.moveToNext()) 
{ 
String contactName = people.getString(people.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 

String contactId = people.getString(people.getColumnIndex(ContactsContract.Contacts._ID)); 
String hasPhone = people.getString(people.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)); 

if ((Integer.parseInt(hasPhone) > 0)) 
{ 

// You know have the number so now query it like this 
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null); 
while (phones.moveToNext()) { 

//store numbers and display a dialog letting the user select which. 
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 

//String numberType = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE)); 

Map<String, String> mapa = new HashMap<String, String>(); 

mapa.put("Name",contactName+"\n"+phoneNumber); 
mapa.put("Phone", phoneNumber); 

/* if(numberType.equals("0")) 
mapa.put("Type", "Work"); 
else 
if(numberType.equals("1")) 
mapa.put("Type", "Home"); 
else if(numberType.equals("2")) 
mapa.put("Type", "Mobile"); 
else 
mapa.put("Type", "Other");*/ 

//Then add this map to the list. 
peopleList.add(mapa); 
} 
phones.close(); 
} 
} 
people.close(); 

startManagingCursor(people); 
} 

//這是我得到讓所有的聯繫人在autocompletetextview在這裏我將顯示我在簡單適配器中使用的row.xml注意:在創建當前上下文時不是row.xml

行XML:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="horizontal" > 
<TextView 
android:id="@+id/text1" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:padding="10dip" > 


</TextView> 

<TextView 
android:id="@+id/text2" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:padding="10dip" > 


</TextView>