私はバンドルを使用して最初のTabFragment
のデータを渡していますが、NullPointerException
と表示されます。エラーは、あなたが実際にあなたの第2のフラグメントを2つ目のタブでlist_fragments2
でtablayout間のバンドルを使用してデータを渡す
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;
}
}
おかげで、私は前にそれを試してみたが、私はまだあなたがlist_fragment2' 'のコードを投稿することができ、このエラー – AnthonyTang
NullPointerExceptionが取得? – beeb
パブリッククラス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