-2

私はバンドルを使用して最初のTabFragmentのデータを渡していますが、NullPointerExceptionと表示されます。エラーは、あなたが実際にあなたの第2のフラグメントを2つ目のタブでlist_fragments2tablayout間のバンドルを使用してデータを渡す

MainActivity断片

list_fragment2 fragment = new list_fragment2(); 
Bundle b = new Bundle(); 
b.putString("test","text"); 
fragment.setArguments(b); 
Toast.makeText(this, "" + b, Toast.LENGTH_SHORT).show(); 

SecondActivity断片

public class list_fragment2 extends Fragment { 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

     View h = inflater.inflate(R.layout.tab2, container, false); 
     TextView textView = (TextView) h.findViewById(R.id.textView); 

     Bundle bundle=getArguments(); 

     //your string 
     if(bundle != null) { 
      String test = bundle.getString("test"); 
      textView.setText(test); 
     } 
      return h; 

    } 
} 

答えて

1

をロードしてくださいするときに発生するのですか?

FragmentManager fragmentManager = getFragmentManager(); 
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 

list_fragment2 fragment = new list_fragment2(); 
Bundle b = new Bundle(); 
b.putString("test","text"); 
fragment.setArguments(b); 
fragmentTransaction.replace(placeholder, fragment); 
fragmentTransaction.commit(); 
+0

おかげで、私は前にそれを試してみたが、私はまだあなたがlist_fragment2' 'のコードを投稿することができ、このエラー – AnthonyTang

+0

NullPointerExceptionが取得? – beeb

+0

パブリッククラスlist_fragment2フラグメント{ @OverrideパブリックビューonCreateView(LayoutInflater用インフレータ、のViewGroup容器、バンドルsavedInstanceState){ ビューH = inflater.inflate(R.layout.tab2、容器、false)を延びています。 TextView textView =(TextView)h.findViewById(R.id.textView); バンドルバンドル= getArguments(); //あなたの文字列 if(bundle!= null){ 文字列テスト= bundle.getString( "test"); textView.setText(test); } return h; }} – AnthonyTang

0

私はあなたがlist_fragment2list_fragment2コードのインスタンスを作成することができると思います。たぶん、あなたのコードは何度もlist_fragment2を作り直しました。

public static list_fragment2 createInstance() { 
     list_fragment2 fragment = new list_fragment2(); 
     Bundle bundle = new Bundle(); 
     bundle .putString("test","text"); 
     fragment.setArguments(bundle); 
     return fragment; 
    } 
関連する問題