实际上,我是将捆绑软件从基本适配器发送到新片段。我如何将束从适配器发送到Frament。 这是我的适配器代码:
textViewItemName.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ExpandableCategoryList fragment = new ExpandableCategoryList();
Bundle bundle = new Bundle();
bundle.putInt("Id",Id);
fragment.setArguments(bundle);
((MainActivity)context).replaceFragment(fragment,
"TAG",null,false);
//Log.e("Hello",fragment.toString());
}
});
我收到的亩捆为
int id = getArguments().getInt("Id",0);
但是我得到了getArguments()。getInt()引发空点异常。我如何处理
答案 0 :(得分:2)
在适配器中,编写代码
ExpandableCategoryList fragmentProductLaptop = new ExpandableCategoryList();
FragmentTransaction fragmentTransaction=context.getSupportFragmentManager().beginTransaction();
Bundle args = new Bundle();
args.putString("Name",category.getCategoryName());
args.putInt("Id",category.getId());
fragmentProductLaptop.setArguments(bundle);
fragmentTransaction.replace(R.id. homeFrameLayout, fragmentProductLaptop);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
在您的Fragment类中,编写以下代码:
public class ExpandableCategoryList extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
String name = getArguments().getString("Name");
String Id = getArguments().getInt("Id",0);
return inflater.inflate(R.layout.fragment, container, false);
}
}