0
  • 我是日本人,所以这个问题可能有语法错误。
    请用简单的英语写下你的答案。对不起。

我正在为安卓开发邮件软件。

我想让邮件应用程序的目的地选择“联系人”,我编写了如下代码。

    button1.setOnClickListener(new View.OnClickListener() {     
        public void onClick(View v) {
            // TODO Method stubs that are automatically generated.
            Intent intent = new Intent(Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_URI);
            startActivityForResult(intent, PICK_CONTACT);
        }
    public void onActivityResult(int reqCode, int resultCode, Intent data){
    super.onActivityResult(reqCode, resultCode, data);
    switch(reqCode){
       case (PICK_CONTACT):
         if (resultCode == Activity.RESULT_OK){


         Cursor c = managedQuery(Uri.parse(data.getDataString()), null, null, null, null);
    if (c.moveToFirst()){

        edittext1.setText( c.getString(
        c.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA1)));

     }
 }
case .....
}
}

然而,作为运行的结果,我引发了 java.lang.IllegalStateException。

01-06 19:28:19.364: E/AndroidRuntime(614): FATAL EXCEPTION: main
01-06 19:28:19.364: E/AndroidRuntime(614): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=3, result=-1, data=Intent { dat=content://com.android.contacts/contacts/lookup/0r1-CF25D709DAA6CF49D22F/1 flg=0x1 (has extras) }} to activity {com.example.voicemailer/com.example.voicemailer.MainActivity}: java.lang.IllegalStateException: get field slot from row 0 col -1 failed
01-06 19:28:19.364: E/AndroidRuntime(614):  at android.app.ActivityThread.deliverResults(ActivityThread.java:2532)
01-06 19:28:19.364: E/AndroidRuntime(614):  at android.app.ActivityThread.handleSendResult(ActivityThread.java:2574)
01-06 19:28:19.364: E/AndroidRuntime(614):  at android.app.ActivityThread.access$2000(ActivityThread.java:117)
01-06 19:28:19.364: E/AndroidRuntime(614):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:961)
01-06 19:28:19.364: E/AndroidRuntime(614):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-06 19:28:19.364: E/AndroidRuntime(614):  at android.os.Looper.loop(Looper.java:123)
01-06 19:28:19.364: E/AndroidRuntime(614):  at android.app.ActivityThread.main(ActivityThread.java:3683)
01-06 19:28:19.364: E/AndroidRuntime(614):  at java.lang.reflect.Method.invokeNative(Native Method)
01-06 19:28:19.364: E/AndroidRuntime(614):  at java.lang.reflect.Method.invoke(Method.java:507)
01-06 19:28:19.364: E/AndroidRuntime(614):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-06 19:28:19.364: E/AndroidRuntime(614):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-06 19:28:19.364: E/AndroidRuntime(614):  at dalvik.system.NativeStart.main(Native Method)
01-06 19:28:19.364: E/AndroidRuntime(614): Caused by: java.lang.IllegalStateException: get field slot from row 0 col -1 failed
01-06 19:28:19.364: E/AndroidRuntime(614):  at android.database.CursorWindow.getString_native(Native Method)
01-06 19:28:19.364: E/AndroidRuntime(614):  at android.database.CursorWindow.getString(CursorWindow.java:329)
01-06 19:28:19.364: E/AndroidRuntime(614):  at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:49)
01-06 19:28:19.364: E/AndroidRuntime(614):  at android.database.CursorWrapper.getString(CursorWrapper.java:135)
01-06 19:28:19.364: E/AndroidRuntime(614):  at com.example.voicemailer.MainActivity.onActivityResult(MainActivity.java:116)
01-06 19:28:19.364: E/AndroidRuntime(614):  at android.app.Activity.dispatchActivityResult(Activity.java:3908)
01-06 19:28:19.364: E/AndroidRuntime(614):  at android.app.ActivityThread.deliverResults(ActivityThread.java:2528)
01-06 19:28:19.364: E/AndroidRuntime(614):  ... 11 more

调试结果,'Cursor c = managedQuery(null, null, null, null)), Uri.parse(data.getDataString();' 原因我想。

但我无法修复它。我知道不推荐 managedQuery() ,但我不知道其他方式。请教我如何解决它....

PS我确实看过这个页面:Cursor cursor = managedQuery(intent.getData().... nullpointerexception 但我不明白。我对该页面的问题是“getContactinfo() 在哪里?以及如何使用它?”。

4

1 回答 1

0

尝试使用 ContentResolver

private static final String[] PROJECTION = new String[] {
ContactsContract.CommonDataKinds.Email.CONTACT_ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Email.DATA
};

...

ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, PROJECTION, null, null, null);
if (cursor != null) {
try {
    final int emailIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA);
    if (cursor.moveToFirst())
     {
    edittext1.setText(cursor.getString(emailIndex));
     }
    } 
finally
  {
    cursor.close();
  }
于 2013-01-06T12:28:58.157 回答