Please help me resolve the following exception?

When I click the button, I want to show the contact list and get the contact number selected by the user. here is my code:

public class MainActivity extends Activity { int level; final int PICK_CONTACT =1; private TextView textView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = new TextView(this); textView.setWidth(100); textView.setHeight(100); @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } public void onClick(View v) { @SuppressWarnings("deprecation") //Contracts class is deprecated , please tell any other alternative if possible. Intent intent= new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI); startActivityForResult(intent, PICK_CONTACT); } @Override public void onActivityResult(int reqCode, int resultCode, Intent data) { super.onActivityResult(reqCode, resultCode, data); switch (reqCode) { case (PICK_CONTACT) : if (resultCode == Activity.RESULT_OK) { Uri contactData = data.getData(); Cursor c = managedQuery(contactData, null, null, null, null); if (c.moveToFirst()) { //here am retrieving name , please tell how to retrieve number as well String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); textView.setText(name); // TODO Fetch other Contact details as you want to use } } break; } } } 

Added permission in manifest file: android.permission.READ_CONTACTS

I don’t know if this is really required or not.

Also tell me if I can ant extract several contacts selected by the user from the contact list.

here is my magazine:

 11-17 15:28:58.265: E/AndroidRuntime(12359): FATAL EXCEPTION: main 11-17 15:28:58.265: E/AndroidRuntime(12359): java.lang.IllegalStateException: Could not execute method of the activity 11-17 15:28:58.265: E/AndroidRuntime(12359): at android.view.View$1.onClick(View.java:2144) 11-17 15:28:58.265: E/AndroidRuntime(12359): at android.view.View.performClick(View.java:2485) 11-17 15:28:58.265: E/AndroidRuntime(12359): at android.view.View$PerformClick.run(View.java:9080) 11-17 15:28:58.265: E/AndroidRuntime(12359): at android.os.Handler.handleCallback(Handler.java:587) 11-17 15:28:58.265: E/AndroidRuntime(12359): at android.os.Handler.dispatchMessage(Handler.java:92) 11-17 15:28:58.265: E/AndroidRuntime(12359): at android.os.Looper.loop(Looper.java:130) 11-17 15:28:58.265: E/AndroidRuntime(12359): at android.app.ActivityThread.main(ActivityThread.java:3687) 11-17 15:28:58.265: E/AndroidRuntime(12359): at java.lang.reflect.Method.invokeNative(Native Method) 11-17 15:28:58.265: E/AndroidRuntime(12359): at java.lang.reflect.Method.invoke(Method.java:507) 11-17 15:28:58.265: E/AndroidRuntime(12359): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) 11-17 15:28:58.265: E/AndroidRuntime(12359): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625) 11-17 15:28:58.265: E/AndroidRuntime(12359): at dalvik.system.NativeStart.main(Native Method) 11-17 15:28:58.265: E/AndroidRuntime(12359): Caused by: java.lang.reflect.InvocationTargetException 11-17 15:28:58.265: E/AndroidRuntime(12359): at java.lang.reflect.Method.invokeNative(Native Method) 11-17 15:28:58.265: E/AndroidRuntime(12359): at java.lang.reflect.Method.invoke(Method.java:507) 11-17 15:28:58.265: E/AndroidRuntime(12359): at android.view.View$1.onClick(View.java:2139) 11-17 15:28:58.265: E/AndroidRuntime(12359): ... 11 more 11-17 15:28:58.265: E/AndroidRuntime(12359): Caused by: java.lang.IllegalArgumentException: URI: content://contacts, calling user: android.uid.system:1000, calling package is one of: [com.sec.android.app.popupuireceiver, com.android.Preconfig, com.sec.app.RilErrorNotifier, com.sec.android.providers.drm, com.android.server.vpn, com.android.systemui, com.android.providers.security, com.wssyncmldm, com.android.settings, com.wssnps, com.android.bluetoothtest, com.sec.android.app.servicemodeapp, com.sec.android.app.factorytest, com.android.settings.mt, com.samsung.InputEventApp, com.sec.dsm.system, com.sec.android.app.wlantest, com.samsung.AlertRecipients, com.sec.android.app.personalization, android, com.sec.android.app.selftestmode, com.google.android.backup, com.android.providers.settings, com.android.providers.subscribedfeeds, com.sec.android.app.lcdtest] 11-17 15:28:58.265: E/AndroidRuntime(12359): at android.os.Parcel.readException(Parcel.java:1326) 11-17 15:28:58.265: E/AndroidRuntime(12359): at android.os.Parcel.readException(Parcel.java:1276) 11-17 15:28:58.265: E/AndroidRuntime(12359): at android.app.ActivityManagerProxy.getProviderMimeType(ActivityManagerNative.java:2846) 11-17 15:28:58.265: E/AndroidRuntime(12359): at android.content.ContentResolver.getType(ContentResolver.java:215) 11-17 15:28:58.265: E/AndroidRuntime(12359): at android.content.Intent.resolveType(Intent.java:3268) 11-17 15:28:58.265: E/AndroidRuntime(12359): at android.content.Intent.resolveTypeIfNeeded(Intent.java:3290) 11-17 15:28:58.265: E/AndroidRuntime(12359): at 11-17 15:28:58.265: E/AndroidRuntime(12359): at com.example.sms.MainActivity.onClick(MainActivity.java:82) 
+4
source share
1 answer

in your onclick button you need to add aim.setType (Phone.CONTENT_TYPE); it is not in your code, please use the following code.

  Intent intent= new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI); intent.setType(Phone.CONTENT_TYPE); startActivityForResult(intent, PICK_CONTACT); 
0
source

Source: https://habr.com/ru/post/1446629/


All Articles