我是android的新手,我已经被困在这个问题中2天了。我的片段无法从我的活动中得到论据。这是我的活动代码。
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);
}
我确信变量号不为空。这是片段中的代码。
@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();
}
查看片段方法。
private void viewFragment() {
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
BottomFragment myFragment = new BottomFragment();
ft.add(R.id.linearLayout, myFragment);
ft.commit();
}
答案 0 :(得分:2)
您的CountUnreadNotifications()方法尚未完成 将其更改为如下并注释掉viewFragment()
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);
FragmentTransaction trans = mManager.beginTransaction();
trans.replace(R.id.fragment_container, fragment ); // change fragment_container to your container
trans.commit();
}
我的猜测是你试图在你的方法viewFragment()中转换到BottomFragment,你必须初始化BottomFragment fragment = new BottomFragment();因此失去了你的论点
请发布viewFragment();