2016-01-20 33 views
-2

我的程序應將編輯文本中識別的電話號碼和電子郵件的值傳遞給另一個活動,該活動分別包含電話號碼和電子郵件的edittext。 我已經設法通過電話號碼和電子郵件的價值逐一。但我失敗時,試圖通過他們在同一時間。用於傳遞多個數據的一個按鈕

\t public void onClickbutton2(View v) 
 
\t { 
 

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

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

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

 
\t }

上述代碼是用於識別從下面的edittext.The碼電話號碼和電子郵件是其中的電話號碼和電子郵件的值應被傳遞到

的意圖

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

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

因此,它是possib當我點擊一個按鈕時,爲了傳遞多個值?爲什麼當我試圖在同一時間傳遞多個值時,我得到了nullpointerexception?我認爲我必須在startactivity()上做些什麼。

+1

你從哪裏得到的空指針?在一個意圖中傳遞2個值時,代碼的外觀如何? – AlbAtNf

+0

你開始兩次活動,你有兩個意圖。刪除第二個。 –

+0

你應該把額外的值放在一個意圖。 – jomartigcal

回答

1

請在以下代碼中查找註釋以瞭解更改。

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="^[email protected]+\\..+$"; 

     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); 

    } 

在另一個活動

String phoneNumber = getIntent().getStringExtra("phones"); 
String emaiId = getIntent().getStringExtra("emails"); 
+0

謝謝!它的工作!我的壞沒有意識到我多次宣佈意圖變量@iSandeep –

相關問題