Bundle传递数据始终为null
创始人
2024-12-27 00:02:30
0次
- 确保将正确的数据类型放入Bundle中。例如,如果尝试放置字符串并使用getInt()来检索它,将返回null。
- 确保从Bundle中检索数据的键与将其放置在Bundle中时使用的键匹配。
- 确保将数据放置在正确的Bundle中。如果数据是从Activity A传递到Fragment B,则应将其放置在Fragment B的Arguments Bundle中而不是Activity A的普通Bundle中。
示例代码:
在Activity A中:
Bundle bundle = new Bundle();
bundle.putString("myKey", "myStringValue");
MyFragment myFragment = new MyFragment();
myFragment.setArguments(bundle);
在MyFragment中:
Bundle bundle = getArguments();
if (bundle != null) {
String myStringValue = bundle.getString("myKey");
}
相关内容