我有一个带有多个按钮的活动,当单击一个按钮时,将打开一个包含两个片段的新活动。
我正尝试在这些片段之一中显示Recyclerview,具体取决于所按下的按钮。问题是捆绑包为空,因此recyclerview不显示。
bundle在onAttach
中不为空,但在settextview
方法中为空
片段
公共类MyFragment扩展了Fragment {
private OnFragmentInteractionListener mListener;
private static TextView input ;
private static RecyclerView rv ;
private Handler mhandler = new Handler();
public Bundle bundle;
public String data;
public MyFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view =inflater.inflate(R.layout.fragment_my, container, false);
input =view.findViewById(R.id.input);
rv =view.findViewById(R.id.rv);
return view;
}
public void setTextView(String text){
input.setText(text);
String[] numberList = input.getText().toString();
final Integer[] numbers = new Integer[numberList.length];
ArrayList<String> List ;
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
rv.setLayoutManager(llm);
ListAdapter adapter = new ListAdapter() ;
rv.setAdapter(adapter);
System.out.println("setextview "+bundle);
if(bundle != null){
if (sort1.equals("se")) {
ClassA m1 = new ClassA();
List = m1.etap(numbers);
adapter.setList(mystepsList);
}else if (sort1.equals("in")) {
ClassB = new ClassB();
List = m1.etap(numbers);
adapter.setList(mystepsList);
} }
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
bundle = getActivity().getIntent().getExtras();
data =b.getString("value");
System.out.println("onAttach "+bundle);
}
public interface OnFragmentInteractionListener {
void onFragmentInteraction(Uri uri);
}}
如果get extra为null或null,就可以,如果再次为null,则可以通过containsKey()交叉检查要获取的密钥,以检查bundle是否具有与此密钥相关的数据。
Bundle bundle = getIntent.getExtras();
if (bundle!=null && bundle.containsKey("value") {
boolean value = bundle.getString("value");
} else {
Log.i("MainActivity", "Bundle is Null or don't have key");
}