0

我正在尝试通过按下按钮来获取电话联系人。但我遇到了以下异常。

06-20 09:23:29.039: I/ApplicationPackageManager(21730): cscCountry is not German : INU
06-20 09:23:46.929: D/AndroidRuntime(21730): Shutting down VM
06-20 09:23:46.929: W/dalvikvm(21730): threadid=1: thread exiting with uncaught exception (group=0x40018578)
06-20 09:23:46.960: E/AndroidRuntime(21730): FATAL EXCEPTION: main
06-20 09:23:46.960: E/AndroidRuntime(21730): java.lang.IllegalArgumentException: URI: content://contacts/, calling user: android.uid.system:1000, calling package is one of: [com.sec.android.app.factorytest, com.sec.android.app.popupuireceiver, com.android.settings.mt, com.android.systemui, com.wssnps, com.sec.android.app.lcdtest, com.samsung.AlertRecipients, com.android.providers.settings, com.android.providers.security, com.samsung.InputEventApp, com.sec.android.providers.drm, android, com.android.Preconfig, com.android.bluetoothtest, com.android.server.vpn, com.google.android.backup, com.sec.android.app.wlantest, com.sec.android.app.personalization, com.sec.app.RilErrorNotifier, com.android.settings, com.wipereceiver, com.android.providers.subscribedfeeds, com.sec.android.app.selftestmode, com.sec.android.app.controlpanel, com.sec.android.app.servicemodeapp]
06-20 09:23:46.960: E/AndroidRuntime(21730):    at android.os.Parcel.readException(Parcel.java:1326)
06-20 09:23:46.960: E/AndroidRuntime(21730):    at android.os.Parcel.readException(Parcel.java:1276)
06-20 09:23:46.960: E/AndroidRuntime(21730):    at android.app.ActivityManagerProxy.getProviderMimeType(ActivityManagerNative.java:2843)
06-20 09:23:46.960: E/AndroidRuntime(21730):    at android.content.ContentResolver.getType(ContentResolver.java:215)
06-20 09:23:46.960: E/AndroidRuntime(21730):    at android.content.Intent.resolveType(Intent.java:3268)
06-20 09:23:46.960: E/AndroidRuntime(21730):    at android.content.Intent.resolveTypeIfNeeded(Intent.java:3290)
06-20 09:23:46.960: E/AndroidRuntime(21730):    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1374)
06-20 09:23:46.960: E/AndroidRuntime(21730):    at android.app.Activity.startActivityForResult(Activity.java:2827)
06-20 09:23:46.960: E/AndroidRuntime(21730):    at com.example.contactpicker.ContentPickerTester$1.onClick(ContentPickerTester.java:29)
06-20 09:23:46.960: E/AndroidRuntime(21730):    at android.view.View.performClick(View.java:2485)
06-20 09:23:46.960: E/AndroidRuntime(21730):    at android.view.View$PerformClick.run(View.java:9080)
06-20 09:23:46.960: E/AndroidRuntime(21730):    at android.os.Handler.handleCallback(Handler.java:587)
06-20 09:23:46.960: E/AndroidRuntime(21730):    at android.os.Handler.dispatchMessage(Handler.java:92)
06-20 09:23:46.960: E/AndroidRuntime(21730):    at android.os.Looper.loop(Looper.java:130)
06-20 09:23:46.960: E/AndroidRuntime(21730):    at android.app.ActivityThread.main(ActivityThread.java:3687)
06-20 09:23:46.960: E/AndroidRuntime(21730):    at java.lang.reflect.Method.invokeNative(Native Method)
06-20 09:23:46.960: E/AndroidRuntime(21730):    at java.lang.reflect.Method.invoke(Method.java:507)
06-20 09:23:46.960: E/AndroidRuntime(21730):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
06-20 09:23:46.960: E/AndroidRuntime(21730):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
06-20 09:23:46.960: E/AndroidRuntime(21730):    at dalvik.system.NativeStart.main(Native Method)

我在下面的代码中遇到异常

和我的ContentPickerTester.java的源代码

startActivityForResult(intent, PICK_CONTACT);

package com.example.contactpicker;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Contacts.People;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class ContentPickerTester extends Activity {

  public static final int PICK_CONTACT = 1;

  @Override
  public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.contentpickertester);

    Button button = (Button)findViewById(R.id.pick_contact_button);

    button.setOnClickListener(new OnClickListener() {
     public void onClick(View _view) {
        Intent intent = new Intent(Intent.ACTION_PICK,
                                   Uri.parse("content://contacts/"));
        startActivityForResult(intent, PICK_CONTACT); 
      }
    });
  }

  @Override
  public void onActivityResult(int reqCode, int resCode, Intent data) {
    super.onActivityResult(reqCode, resCode, data);

    switch(reqCode) {
      case (PICK_CONTACT) : {
        if (resCode == Activity.RESULT_OK) {
          Uri contactData = data.getData();
          Cursor c = managedQuery(contactData, null, null, null, null);
          c.moveToFirst();
          String name = c.getString(c.getColumnIndexOrThrow(People.NAME));
          TextView tv = (TextView)findViewById(R.id.selected_contact_textview);
          tv.setText(name);
        }
        break;
      }
    }
  }
}

我在下面的代码中遇到异常

4

3 回答 3

2

试试下面的代码:

Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);  
startActivityForResult(intent, 1);  

// ...

@Override  
public void onActivityResult(int reqCode, int resultCode, Intent data) {  
    super.onActivityResult(reqCode, resultCode, data);  
    if (resultCode == Activity.RESULT_OK) {  
        Uri contactData = data.getData();  
        Cursor c =  managedQuery(contactData, null, null, null, null);  
        if (c.moveToFirst()) {  
            String name = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));  
           TextView tv = (TextView)findViewById(R.id.selected_contact_textview);
      tv.setText(name);
        }  
    }  
}
于 2013-06-20T04:21:22.497 回答
1

你可以通过intent这种方式。

Intent intent = new Intent(Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_URI);
        startActivityForResult(intent, 0);

public void onActivityResult(int reqCode, int resultCode, Intent data) {
        super.onActivityResult(reqCode, resultCode, data);
        if (i == reqCode && resultCode == Activity.RESULT_OK) {
            Cursor c = managedQuery(contactData, null, null, null, null);
                    if (c.moveToFirst()) {
                        String id = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
                        String hasPhone = c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

                        if (hasPhone.equalsIgnoreCase("1")) {
                            Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                                            null,
                                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                                    + " = " + id, null, null);
                            phones.moveToFirst();
                            cNumber = phones.getString(phones.getColumnIndex("data1"));
                            name=phones.getString(phones.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
                            System.out.println("NAME:"+name+"\n NUMBER:"+cNumber);

                    }
                    }
           }
        }

它工作得很好,我总是用这个。

于 2013-06-20T04:28:46.723 回答
0

将以下行包含到您的 AndroidManifest.xml 下

<uses-permission android:name="android.permission.READ_CONTACTS" />
于 2013-06-20T04:05:22.830 回答