0

我从Beginning Android™ Application Development Book 中复制代码示例。该代码用于在用户单击按钮时根据我的应用程序上的电话号码显示联系人列表。当我评论// startActivity(i); on onActivityResult(int requestCode, int resultCode, Intent data) function我得到以下字符串时,代码工作正常

内容://com.android.contacts/data/1

但我想从我的联系人列表中获取电话号码。为此,我使用以下代码。

public OnClickListener addContactButtonListener = new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Log.v(TAG, "addContactButtonListener  ");
        Intent i = new Intent(android.content.Intent.ACTION_PICK);
        i.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
        startActivityForResult(i, 1);


    }
};

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.v(TAG, "onActivityResult");

    if (requestCode == 1) {
        Log.v(TAG, "onActivityResult > requestCode " + requestCode);

        if (resultCode == RESULT_OK) {
            Log.v(TAG, "onActivityResult > resultCode " + resultCode);
            Toast.makeText(this, data.getData().toString(),
                    Toast.LENGTH_SHORT).show();

            queryEditText.setText(data.getData().toString());
            Intent i = new Intent(android.content.Intent.ACTION_VIEW,
                    Uri.parse(data.getData().toString()));

            Log.v(TAG, "onActivityResult > startActivity " + i.toString());

            startActivity(i);
        }
    }
}

但是在取消评论时startActivity(i); 我得到了以下错误

11-09 23:09:37.913: V/MyApp_Main(4002): onActivityResult > startActivity Intent { act=android.intent.action.VIEW dat=content://com.android.contacts/data/1 }
11-09 23:09:37.923: D/AndroidRuntime(4002): Shutting down VM
11-09 23:09:37.923: W/dalvikvm(4002): threadid=1: thread exiting with uncaught exception (group=0x40015560)
11-09 23:09:37.933: E/AndroidRuntime(4002): FATAL EXCEPTION: main
11-09 23:09:37.933: E/AndroidRuntime(4002): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://com.android.contacts/data/1 flg=0x1 (has extras) }} to activity {com.example.MyApp/com.example.MyApp.MyApp_Main}: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=content://com.android.contacts/data/1 }
11-09 23:09:37.933: E/AndroidRuntime(4002):     at android.app.ActivityThread.deliverResults(ActivityThread.java:2532)
11-09 23:09:37.933: E/AndroidRuntime(4002):     at android.app.ActivityThread.handleSendResult(ActivityThread.java:2574)
11-09 23:09:37.933: E/AndroidRuntime(4002):     at android.app.ActivityThread.access$2000(ActivityThread.java:117)
11-09 23:09:37.933: E/AndroidRuntime(4002):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:961)

谁能解释这里发生了什么?

4

0 回答 0