我有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