【发布时间】:2017-06-20 08:20:19
【问题描述】:
我有 3 个片段。片段 A、B 和 C。A 有一个“继续”按钮,它将把它带到 B。B 有一个继续按钮,它将把它带到 C。C 有一个“添加”按钮,它将把它带回 B。现在我想要在按下继续按钮时将数据从 A 发送到 B。以及按下添加按钮时从 C 到 B。我尝试使用捆绑包。第一次从 A 到 B 时,它给了我空指针异常,来自 C 的包为空。如何解决这个问题?非常感谢任何帮助。请通过下面的代码sn-p
注意:ItemDetails是从fragment A获取的,EmployeeDetails是从fragment C获取的。 Fragment Flow => 1. fragment 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
@覆盖 public void 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
-
你能发布你的捆绑代码,发送和接收吗?
-
Fragments之间的通信,可以circulate via Activity或者关注this answer
-
您可以将设置参数的部分添加到 Fragment 吗?
-
@Eselfar 完成..