I am new to android and i've been stucked in this problem for 2 days. My fragment cannot get the argument from my activity. here is my code in activity.
我是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);
}
I am sure that the variable number is not null. And here is my code in the fragment.
我确信变量号不为空。这是片段中的代码。
@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);
Please can somebody help me to answer this question. Thanks. Here is my code in Activity Oncreate
请有人帮我回答这个问题。谢谢。这是我在Activity Oncreate中的代码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu_page);
mDB.Open();
CountUnreadNotifications();
viewFragment();
}
View Fragment method.
查看片段方法。
private void viewFragment() {
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
BottomFragment myFragment = new BottomFragment();
ft.add(R.id.linearLayout, myFragment);
ft.commit();
}
1 个解决方案
#1
2
Your CountUnreadNotifications() method is not complete.
Change it as below and comment out viewFragment()
您的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();
}
My guess is your trying to transit to BottomFragment in your method viewFragment() where you have to initialize BottomFragment fragment = new BottomFragment(); againg thus loosing your arguments.
Please post viewFragment();
我的猜测是你试图在你的方法viewFragment()中转换到BottomFragment,你必须初始化BottomFragment fragment = new BottomFragment();因此失去了你的论点。请发布viewFragment();
#1
2
Your CountUnreadNotifications() method is not complete.
Change it as below and comment out viewFragment()
您的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();
}
My guess is your trying to transit to BottomFragment in your method viewFragment() where you have to initialize BottomFragment fragment = new BottomFragment(); againg thus loosing your arguments.
Please post viewFragment();
我的猜测是你试图在你的方法viewFragment()中转换到BottomFragment,你必须初始化BottomFragment fragment = new BottomFragment();因此失去了你的论点。请发布viewFragment();