我已经按照谷歌教程进行操作并没有在我的代码中出现任何错误,但由于某种原因,我的捆绑包中的数据似乎没有从我的主活动发送到我的片段(我的应用程序只是一直返回“ 0.00" )。请帮忙。
主要活动:
@Override
public void assignButtonBWHW1Clicked(String assignButtonBWHWClicked) {
if (assignButtonBWHWClicked.equals(assignButtonBWHWClicked)) {
assignFragment1 = new assignFragment1();
buttonsGeneric = new genericBackNextFragment();
splitItFragmentManager3 = getFragmentManager();
splitItFragmentTransaction3 = splitItFragmentManager3.beginTransaction();
splitItFragmentTransaction3.replace(R.id.fragment_container, assignFragment1);
splitItFragmentTransaction3.replace(R.id.fragment_container_2, buttonsGeneric);
splitItFragmentTransaction3.addToBackStack(null);
splitItFragmentTransaction3.commit();
Bundle bundleAssign = new Bundle();
bundleAssign.putDouble("price1", 22);
assignFragment1.setArguments(bundleAssign);
}
}
FRAGMENT:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
assignFragment1View = inflater.inflate(R.layout.assign_fragment_1, container, false);
item1 = (TextView) assignFragment1View.findViewById(R.id.assign1_food_item_1);
item2 = (TextView) assignFragment1View.findViewById(R.id.assign1_food_item_2);
priceItem1 = (TextView) assignFragment1View.findViewById(R.id.assign1_price_item_1);
priceItem2 = (TextView) assignFragment1View.findViewById(R.id.assign1_price_item_2);
Bundle bundleAssign = this.getArguments();
Double price1Double = bundleAssign.getDouble("priceItem1", 0.00);
priceItem1.setText(String.valueOf(price1Double));
return assignFragment1View;
}
答案 0 :(得分:1)
只需将key
中的fragment
从priceItem1
更新为price1
。
Double price1Double = bundleAssign.getDouble("priceItem1", 0.00);
到
Double price1Double = bundleAssign.getDouble("price1", 0.00);
要明确这一点。发生这种情况是因为您在使用不同的密钥添加数据以进行捆绑和检索时使用单独的密钥。所以它返回default value
,0.00
为priceItem1
键
答案 1 :(得分:1)
您必须在代码中进行两项更改。
第一项是在交易前设置参数
if (assignButtonBWHWClicked.equals(assignButtonBWHWClicked)) {
assignFragment1 = new assignFragment1();
Bundle bundleAssign = new Bundle();
bundleAssign.putDouble("price1", 22);
assignFragment1.setArguments(bundleAssign);
buttonsGeneric = new genericBackNextFragment();
splitItFragmentManager3 = getFragmentManager();
splitItFragmentTransaction3 = splitItFragmentManager3.beginTransaction();
splitItFragmentTransaction3.replace(R.id.fragment_container, assignFragment1);
splitItFragmentTransaction3.replace(R.id.fragment_container_2, buttonsGeneric);
splitItFragmentTransaction3.addToBackStack(null);
splitItFragmentTransaction3.commit();
}
和第二个总是使用相同的键来设置和检索包中的数据,因此更改片段
Double price1Double = bundleAssign.getDouble("priceItem1", 0.00);
到
Double price1Double = bundleAssign.getDouble("price1", 0.00);
答案 2 :(得分:0)
尝试
Bundle bundleAssign = getArguments();
或者,
Bundle bundleAssign = getActivity().getArguments()
而不是
Bundle bundleAssign = this.getArguments();
获得价值
double result = bundleAssign.getDouble("price1");
答案 3 :(得分:0)
在获得片段nmanager之前,尝试将bundle初始化放入片段。
if (assignButtonBWHWClicked.equals(assignButtonBWHWClicked)) {
assignFragment1 = new assignFragment1();
buttonsGeneric = new genericBackNextFragment();
Bundle bundleAssign = new Bundle();
bundleAssign.putDouble("price1", 22);
assignFragment1.setArguments(bundleAssign);
splitItFragmentManager3 = getFragmentManager();
splitItFragmentTransaction3 = splitItFragmentManager3.beginTransaction();
splitItFragmentTransaction3.replace(R.id.fragment_container, assignFragment1);
splitItFragmentTransaction3.replace(R.id.fragment_container_2, buttonsGeneric);
splitItFragmentTransaction3.addToBackStack(null);
splitItFragmentTransaction3.commit();
}