在字符串中存储联系人姓名

时间:2016-08-10 10:53:05

标签: android android-intent contacts

当我尝试将从Contact Book中选择的联系人名称存储到TextView中时,我不断遇到以下崩溃。我不确定这里有什么错误,因为Toast会显示存储在字符串变量中的联系人姓名' name'但是当我尝试TextView.setText(name)时,我得到一个空指针异常?

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://com.android.contacts/contacts/lookup/0r1-4F314D4F514D314B454331/1 flg=0x1 }} to activity {com.example.calvin.pickmeup/com.example.calvin.pickmeup.Home}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(int)' on a null object reference

主页:

private TextView Address;
private TextView selectedContact;
private String name;
private final int PICK_CONTACT = 1;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.home);

    Button currentLocation = (Button) findViewById(R.id.currentLocation);
    Button chooseTime = (Button) findViewById(R.id.chooseTime);
    Button chooseContact = (Button) findViewById(R.id.chooseContact);
    TextView Address = (TextView) findViewById(R.id.Address);
    TextView selectedContact = (TextView) findViewById(R.id.selectedContact);
    final TextView selectedTime = (TextView) findViewById(R.id.selectedTime);

    currentLocation.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Home.this, MapsActivity.class);
            startActivity(intent);
        }
    });

    chooseTime.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v) {
            Calendar mcurrentTime = Calendar.getInstance();
            int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
            int minute = mcurrentTime.get(Calendar.MINUTE);
            TimePickerDialog mTimePicker;
            mTimePicker = new TimePickerDialog(Home.this, new TimePickerDialog.OnTimeSetListener() {
                @Override
                public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
                    selectedTime.setText( selectedHour + ":" + selectedMinute);
                }
            }, hour, minute, true);//Yes 24 hour time
            mTimePicker.setTitle("Select Time");
            mTimePicker.show();
        }
    });

    Bundle extras = getIntent().getExtras();
    if(extras != null) {
        String value = extras.getString("address");
        Address.setText(value);
    }
}

public void callContacts(View v){

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

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(requestCode == PICK_CONTACT) {
        if(resultCode == ActionBarActivity.RESULT_OK){
            Uri contactData = data.getData();
            Cursor c = getContentResolver().query(contactData, null,null,null,null);
            if (c.moveToFirst()){
                name = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME_PRIMARY));
                Toast.makeText(this,"You've Picked:" + name, Toast.LENGTH_LONG).show();
                selectedContact.setText(name);
            }
        }
    }

}

问题在于:

selectedContact.setText(名称);

2 个答案:

答案 0 :(得分:0)

您没有初始化全局变量并创建另一个局部变量删除TextView

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home);

        Button currentLocation = (Button) findViewById(R.id.currentLocation);
        Button chooseTime = (Button) findViewById(R.id.chooseTime);
        Button chooseContact = (Button) findViewById(R.id.chooseContact);
        TextView Address = (TextView) findViewById(R.id.Address);
// remove TextView
         selectedContact = (TextView) findViewById(R.id.selectedContact);
        final TextView selectedTime = (TextView) findViewById(R.id.selectedTime);

        currentLocation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Home.this, MapsActivity.class);
                startActivity(intent);
            }
        });

答案 1 :(得分:0)

您正在定义一个全局变量:

private TextView Address;
private TextView selectedContact;

更改此代码:

TextView Address = (TextView) findViewById(R.id.Address);
TextView selectedContact = (TextView) findViewById(R.id.selectedContact);

并将此值设置为已定义的全局变量:

Address = (TextView) findViewById(R.id.Address);
selectedContact = (TextView) findViewById(R.id.selectedContact);