一个用于传递多个数据的按钮

时间:2016-01-20 14:22:28

标签: java android android-studio

我的程序应将电话号码和从editText识别的电子邮件的值传递到另一个活动中,该活动分别包含电话号码和电子邮件的edittext。 我已经设法通过电话号码和电子邮件的价值逐一传递。但是当我试图同时传递它们时失败了。

	public void onClickbutton2(View v) 
	{

		/*----------------------------------phone number catcher------------------*/
	
		String text = _field.getText().toString();
		Pattern pt = Pattern.compile("\\d{5,11}");
		Matcher m = pt.matcher(text);
		StringBuilder sb = new StringBuilder();
		while (m.find()) {
			sb.append(m.group()).append(",");
		}
		String[] phones = sb.toString().split(",");
		Intent i = new Intent(this, Contacts.class);
		i.putExtra("phones", phones);
		startActivity(i);

		/*------------------------------------------------email catcher-----------*/
      
		String email = _field.getText().toString();
		String EMAIL_PATTERN="^.+@.+\\..+$";

		Pattern pp = Pattern.compile(EMAIL_PATTERN);
		Matcher e = pp.matcher(email);
		StringBuilder sc = new StringBuilder();
		while (e.find()) {
			sc.append(e.group()).append(",");
		}
		String[] emails = sc.toString().split(",");
		Intent j = new Intent(this, Contacts.class);
		j.putExtra("emails", emails);
		startActivity(j);

	}

上述代码用于识别编辑文本中的电话号码和电子邮件。以下代码是电话号码和电子邮件的值应传递到的意图

        Intent phone=getIntent();
        String [] phones = phone.getStringArrayExtra("phones");
        edtTxtContactNumber.setText(phones[0]);

        Intent email=getIntent();
        String [] emails = email.getStringArrayExtra("emails");
        edtTxtContactEmail.setText(emails[0]);

那么在单击一个按钮时可以传递多个值吗?为什么我在尝试同时传递多个值时会得到nullpointerexception?我认为我必须在startactivity()中做一些事情。任何想法?

1 个答案:

答案 0 :(得分:1)

在以下代码中查找注释以了解更改。

print $fh $rr->string;

关于其他活动

public void onClickbutton2(View v) 
    {

        /*----------------------------------phone number catcher------------------*/
        Intent i = new Intent(this, Contacts.class);   //<-- Declare your Intent variable here.
        String text = _field.getText().toString();
        Pattern pt = Pattern.compile("\\d{5,11}");
        Matcher m = pt.matcher(text);
        StringBuilder sb = new StringBuilder();
        while (m.find()) {
            sb.append(m.group()).append(",");
        }
        String[] phones = sb.toString().split(",");

        i.putExtra("phones", phones);   //<-- add Phone number to intent

        /*------------------------------------------------email catcher-----------*/

        String email = _field.getText().toString();
        String EMAIL_PATTERN="^.+@.+\\..+$";

        Pattern pp = Pattern.compile(EMAIL_PATTERN);
        Matcher e = pp.matcher(email);
        StringBuilder sc = new StringBuilder();
        while (e.find()) {
            sc.append(e.group()).append(",");
        }
        String[] emails = sc.toString().
        i.putExtra("emails", emails);    //<-- add email to Intent
        startActivity(i);

    }