2016-02-28 68 views
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(); 


     }}); 
+0

如何打開你的第二個片段? – yennsarah

+0

一個按鈕: 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(); }}); –

+0

請編輯你的問題,而不是...大的代碼塊在評論部分不是真正可讀的。 – yennsarah

回答

1

您要更換你的第一個FragmentFragment_2,而不是具有Bundle之一。 聲明你Fragment_2上述onCreate(全球),實例化它:

fragment = new Fragment_2(); 

,改變你的replace行:

fragmentTransaction.replace(R.id.content_frame, fragment); 
+0

這是我的問題。謝謝。 –

相關問題