我有3個片段。片段A,B和C.A有一個「繼續」按鈕,它將把它帶到B.B有一個繼續按鈕,它將把它帶到C. C有一個「添加」按鈕,它將把它帶回B.現在我想要在按下繼續按鈕時將數據從A發送到B.當按下添加按鈕時也從C到B.我嘗試使用捆綁。它從A到B第一次給我空指針異常,C中的包爲空。如何解決這個問題?任何幫助,高度讚賞。請仔細閱讀下面的代碼片段3個片段之間的通信
注意:ItemDetails從片段A獲得,而EmployeeDetails從片段C獲得。Fragment Flow => 1.片段A 2. A到B(itemsList傳遞給B)3. B到C(無通訊)4.從C返回B(傳遞給B的員工列表)。
String TEMP_STRING_EMPLOYEES, TEMP_STRING_ITEMS;
EmployeeList employeeList;
ItemsList itemsList;
@Override
public void onStart() {
super.onStart();
Bundle args = getArguments();
if (args != null) {
TEMP_STRING_ITEMS = args.getString("ItemsDetails");
try {
// Set article based on argument passed in
TEMP_STRING_EMPLOYEES = args.getString("EmployeeDetails");
} catch (NullPointerException ex) {
}
} else {
}
}
//Next lines of code from MAinActivity.java
@覆蓋 公共無效onFragmentInteractionForEmployeeDetails(ArrayList中的ArrayList){
EmployeeList employeeList = new EmployeeList(arrayList);
String correspondingJson = NavigationUtils.getStringForObject(employeeList);
B newFragment = new B();
Bundle args = new Bundle();
args.putString("EmployeeDetails", correspondingJson);
newFragment.setArguments(args);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack so the user can navigate back
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
transaction.commit();
}
看看[this](https://stackoverflow.com/a/44008851/6950238) –
你可以發佈你的捆綁代碼,發送和接收? –
對於片段之間的通信,您可以[通過活動](https://developer.android.com/training/basics/fragments/communicating.html)進行傳播或遵循[本答案](https://stackoverflow.com/ a/36496481/1276636) – Sufian