我对android还不熟悉,我已经在这个问题上困了两天了。我的片段无法从我的活动中获得论点。这是我的活动代码。
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();
}
最佳答案
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();