0
我有兩個片段,我試圖在它們之間傳遞一個整數。在第一個,在Fragment_1.java我有這樣的的onCreate:Android Bundle Nullpointerexception
pontszam = 12;
Fragment fragment = new Fragment_2();
Bundle bundle = new Bundle();
bundle.putInt("pont", pontszam);
fragment.setArguments(bundle);
然後在第二個片段,在Fragment_2.java我有這樣的的onCreate:
Bundle bundle = this.getArguments();
int myInt = bundle.getInt("pont");
TextView pontjelzo = (TextView)rootView.findViewById(R.id.pontszam_2);
pontjelzo.setText(Integer.toString(myInt));
,但我得到空指針異常在這條線上:
int myInt = bundle.getInt("pont");
我在做什麼錯?
這是我打開我的第二個片段:
button_1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_left).replace(R.id.content_frame, new Fragment_2());
fragmentTransaction.commit();
}});
如何打開你的第二個片段? – yennsarah
一個按鈕: button_1.setOnClickListener(新View.OnClickListener(){ @Override 公共無效的onClick(視圖v){ FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.setCustomAnimations(R.anim.enter_from_right,R.anim.exit_to_left).replace(R.id.content_frame,new Fragment_2()); fragmentTransaction.commit(); }}); –
請編輯你的問題,而不是...大的代碼塊在評論部分不是真正可讀的。 – yennsarah