-1
  • 我在我的活动中使用了选择联系人功能。它可以很好地选择联系人和其他详细信息,如姓名。
  • 但是当我单击按钮时,它会显示联系意图并显示数字一切都很好。
  • 但是,如果我没有选择数字并单击后按意味着应用程序强制关闭。

代码

Intent inten = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(inten,PICK_CONTACT);

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

    Cursor c;
    switch (reqCode) {
    case (PICK_CONTACT):

        Uri result = data.getData();
    Log.v("TAG", "Got a result: " + result.toString());

    // get the contact id from the Uri
    String id = result.getLastPathSegment();

    // query for phone numbers for the selected contact id
    c = getContentResolver().query(
            Phone.CONTENT_URI, null,
            Phone.CONTACT_ID + "=?",
            new String[]{id}, null);

    int phoneIdx = c.getColumnIndex(Phone.NUMBER);
    int phoneType = c.getColumnIndex(Phone.TYPE);

    if(c.getCount() > 1) { // contact has multiple phone numbers
        final CharSequence[] numbers = new CharSequence[c.getCount()];
        int i=0;
        if(c.moveToFirst()) {
            while(!c.isAfterLast()) { // for each phone number, add it to the numbers array
                String type = (String) Phone.getTypeLabel(this.getResources(), c.getInt(phoneType), ""); // insert a type string in front of the number
                String number = type + ": " + c.getString(phoneIdx);
                numbers[i++] = number;
                c.moveToNext();
            }
            // build and show a simple dialog that allows the user to select a number
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("Select Number");
            builder.setItems(numbers, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int item) {
                    String number = (String) numbers[item];
                    int index = number.indexOf(":");
                    number = number.substring(index + 2);
                    cNumber=number;
                    edt.setText(cNumber);
                    //Toast.makeText(getApplicationContext(), number, Toast.LENGTH_LONG).show();
                    // loadContactInfo(number); // do something with the selected number
                }
            });
            AlertDialog alert = builder.create();
            alert.setOwnerActivity(this);
            alert.show();

        } else Log.w("TAG", "No results");
    } else if(c.getCount() == 1) {
        // contact has a single phone number, so there's no need to display a second dialog

        if(c.moveToFirst())
        {
            cNumber = c.getString(c.getColumnIndex("data1"));
            edt.setText(cNumber);
            //Toast.makeText(getApplicationContext(), cNumber, Toast.LENGTH_LONG).show();
        }
    }

    break;
    }

}

在我的活动中,我还写了回压...

@Override
public void onBackPressed() {

    btnflag++;
    if(btnflag==1)
    {
        Intent mainscr = new Intent(FindList1.this, MainScreen.class);
        startActivity(mainscr);
        btnflag=0;
        finish();
    }
}

但不幸的是,事故发生了......

4

1 回答 1

1

当用户单击返回按钮时,它将调用 activityResult()。我猜你在这段代码中得到了空指针异常。

Uri result = data.getData();
Log.v("TAG", "Got a result: " + result.toString());

// get the contact id from the Uri
String id = result.getLastPathSegment();

// query for phone numbers for the selected contact id
c = getContentResolver().query(
Phone.CONTENT_URI, null,
Phone.CONTACT_ID + "=?",
new String[]{id}, null);

int phoneIdx = c.getColumnIndex(Phone.NUMBER);
int phoneType = c.getColumnIndex(Phone.TYPE);

data.getData() 它将返回 null。因为您没有选择任何联系人并且您在联系人活动中按下了返回按钮。

if(data==null){ return; }

并处理异常

onBackPressed() 方法将在当前活动按下返回按钮时调用。当您进行其他活动时,它不会被调用。

于 2013-04-13T05:16:18.067 回答