我有两个片段,我试图在它们之间传递一个整数。在第一个版本中,在Fragment_1.java中,我在onCreate中有这样的内容:
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));但我在这一行中得到了null指针异常:
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 ();
}});发布于 2016-02-28 11:20:15
您将用一个Fragment替换第一个 Fragment_2,而不是一个有Bundle的Fragment_2。在Fragment_2(全局)之上声明您的onCreate,实例化它:
fragment = new Fragment_2();并更改您的replace行:
fragmentTransaction.replace(R.id.content_frame, fragment);https://stackoverflow.com/questions/35681544
复制相似问题