我是Android开发新手,最近遇到了一个问题。我的Fragment无法从Activity中获取参数。以下是我的活动代码。
private void CountUnreadNotifications() {
Cursor unread = db.getUnread();
Bundle bundle = new Bundle();
String number = Integer.toString(unread.getCount());
bundle.putString("noOfNotif", number);
BottomFragment fragment = new BottomFragment();
fragment.setArguments(bundle);
}
我确定变量number不为空。以下是我的片段代码。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String i = null;
Bundle bundle = this.getArguments();
if(bundle != null){
i = bundle.getString("noOfNotif");
}
else {
i = "0";
}
View view = inflater.inflate(R.layout.fragment_menu_page, container,
false);
txtnoNotif = (TextView) view.findViewById(R.id.txtnoNotif);
txtnoNotif.setText(i);
请问有人可以帮助我回答这个问题吗?谢谢。 以下是我的Activity Oncreate中的代码。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu_page);
mDB.Open();
CountUnreadNotifications();
viewFragment();
}
查看Fragment方法。
private void viewFragment() {
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
BottomFragment myFragment = new BottomFragment();
ft.add(R.id.linearLayout, myFragment);
ft.commit();
}