2017-08-04 33 views
0

我已經試過指類似問題發出的一個片段中獲取數據,但我無法得到解決。其實我想一個字符串發送從一個片段到另一個,但在接下來的片段,同時接收數據它顯示零點異常錯誤。我不是從另一個片段

我的代碼:

public void onClick(View view) { 
switch(view.getId()){ 
case R.id.proceed_button: 
      user_email=editText.getText().toString().trim(); 
      frag_two si=new frag_two(); 
      Bundle args=new Bundle(); 
      args.putString("this_email",user_email); 
      si.setArguments(args); 
      break;}} 
public String sendString(){ 
    return emailT.getText().toString().trim(); 
} 

容器活動: (內部的方法) 斷枝GI =新FRAG(); 字符串EMAIL_ID = gi.sendString();

+0

相反字符串EMAIL_ID = gi.sendString();做gi.sendString(email_id);但對於你的問題的接口是答案 – 2017-08-04 03:18:00

回答

1

編輯

對於幀間片段或片段活動的溝通,你應該使用的界面和回調監聽像this

的活動,以片段:

在你的onCreate試試這個()方法,請查詢this獲取更多信息

Bundle bundle = this.getArguments(); 
if (bundle != null) { 
    String myString = bundle.getString("this_email"); 
} 

簡單的方法,我想通過它在我的構造函數或構造的情況下,它正在過載後創建的方法。

public class TestFragment extends Fragment { 

private String mString; 

public TestFragment { 

} 

public void sendString(String str){ 
    this.mString = str; 

} 

}

而在活動,

TestFragment testFragment = new TestFragment(); 
testFragment.sendString(user_email); 
# add other codes 
+0

先生,我試圖複製你在某些方面,但仍顯示錯誤,請檢查我的編輯 –

+0

嘗試接口從片段發送到活動 – 2017-08-04 03:15:53

+0

其實我有點在使用界面困惑。因此,這將是真正有用的,如果你能證明在回答有點 –