【问题标题】:null bundle in fragment , what is the right way to get a bundle in fragment?片段中的空包,在片段中获取包的正确方法是什么?
【发布时间】:2020-07-27 23:38:04
【问题描述】:

我有一个带有多个按钮的活动,当单击一个按钮时,会打开一个带有两个片段的新活动。

我正在尝试根据按下的按钮在其中一个片段中显示 Recyclerview。 问题是捆绑包是空的,所以 recyclerview 不显示。

bundle 在onAttach 中不为空,但在settextview 方法中为空

片段

公共类 MyFragment 扩展片段 {

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();
    sort1 =b.getString("value");
    System.out.println("onAttach "+bundle);
}
public interface OnFragmentInteractionListener {

    void onFragmentInteraction(Uri uri);
}}

【问题讨论】:

    标签: android android-studio android-fragments android-recyclerview bundle


    【解决方案1】:

    如果 get extra 为 null 或 null,则可以,如果再次不为 null,则可以通过 containsKey() 与要获取的密钥进行交叉检查,以检查 bundle 是否具有与此密钥相关的数据。

    Bundle bundle = getIntent.getExtras();
        if (bundle!=null && bundle.containsKey("value") {
                boolean value = bundle.getStringExtra("value");
        } else {
            Log.i("MainActivity", "Bundle is Null or don't have key");
    
        }
    

    【讨论】:

      【解决方案2】:

      尝试使用此代码将数据从活动传递到片段

      活动中

      Bundle bundle = new Bundle();
      bundle.putString("edttext", "From Activity");
      // set Fragmentclass Arguments
      Fragmentclass fragobj = new Fragmentclass();
      fragobj.setArguments(bundle);
      

      在片段中

      捆绑包= getArguments();

      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
              Bundle savedInstanceState) {
           Bundle bundle= getArguments();
            if(bundle!=null){
               String strtext = bundle.getString("edttext"); 
             }
      
          return inflater.inflate(R.layout.fragment, container, false);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-09-10
        相关资源
        最近更新 更多