Problem with opening contacts - Android

2019-05-21 10:58发布

I've put a feature in my app that opens the contacts list. The problem is that some users reported that the app crashed when they tried to use it. The feature seems to works fine for most people(me included, with Nexus S).

Here's the code I've used to open the contacts -

    call_friend.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            Intent i = new Intent();
            i.setComponent(new ComponentName("com.android.contacts", "com.android.contacts.DialtactsContactsEntryActivity"));
            i.setAction("android.intent.action.MAIN");
            i.addCategory("android.intent.category.LAUNCHER");
            i.addCategory("android.intent.category.DEFAULT");
            startActivity(i);
        }
    }); 

Here's the log of the crash -

            android.content.ActivityNotFoundException: Unable to find explicit activity class {com.android.contacts/com.android.contacts.DialtactsContactsEntryActivity}; have you declared this activity in your AndroidManifest.xml?
            at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1404)
            at android.app.Instrumentation.execStartActivity(Instrumentation.java:1378)
            at android.app.Activity.startActivityForResult(Activity.java:2817)
            at android.app.Activity.startActivity(Activity.java:2923)
            at can.you.drive.dont_drive$1.onClick(dont_drive.java:75)
            at android.view.View.performClick(View.java:2465)
            at android.view.View$PerformClick.run(View.java:8907)
            at android.os.Handler.handleCallback(Handler.java:587)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            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:858)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
            at dalvik.system.NativeStart.main(Native Method)

I don't know what's causing it. Thanks!

3条回答
萌系小妹纸
2楼-- · 2019-05-21 11:12

Since you indicated above that the Galaxy S is experiencing the problem, you should take a look at what would appear to be the Android Manifest for the Contacts app on the Galaxy S here. Looking at this, there's no DialtactsContactsEntry Activity, which would explain what you're seeing. Based on the manifest as well as this message on another forum, what you would need to use in this case is "com.sec.android.app.contacts.PhoneBookTopMenuActivity". This just covers the case of the Galaxy S - other devices that have their own Contacts replacement will likely have something completely different and there's no guarantee that the next rev of the Galaxy line won't choose to change this.

Of course (and I'm sure this is not news to you), the whole exercise underscores the inherent problems with using undocumented APIs/application features... the better approach is to use something like what ErikR described in his answer.

查看更多
家丑人穷心不美
3楼-- · 2019-05-21 11:31

I think this is because the Android change the number for this App. I had a similar problem if I used

intent.setClassName("com.android.contacts","com.android.contacts.DialtactsActivity");

it could run at Android 4.1 but not at 4.4. In 4.4 I had to change it like this

intent.setClassName("com.google.android.dialer","com.android.dialer.DialtactsActivity");
查看更多
别忘想泡老子
4楼-- · 2019-05-21 11:33

Use an implicit Intent to launch the Contacts activity - i.e. tell the OS you want to view a list of contacts, and it will figure out the right activity to use (or prompt the user if more than one Contacts app is installed). The following Intent will do the trick:

Intent i = new Intent();
i.setAction(Intent.ACTION_VIEW);
i.setData(Uri.parse("content://contacts/people/"));
startActivity(i);

Try to avoid explicit Intents whenever possible... too many different devices out there to be absolutely sure that a particular package/activity will always exist.

查看更多
登录 后发表回答