在将数据通过捆绑包从活动传递到片段时获取 null

通过捆绑包传递时在途中丢失数据,我得到


尝试在 String bundlePassData = this.getArguments().getString(“tagName”) 上的空对象引用上调用虚拟方法 'java.lang.String.String android.os.bundle.getString(java.lang.String)'


这是我的代码示例。主要活动->


 tagsView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override

        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

            Bundle bundle = new Bundle();

            TextView textView = (TextView) view.findViewById(R.id.tagName);

            String selectedItemName = textView.getText().toString();

            bundle.putString("tagName", selectedItemName);


            TaskAssignFragment taskAssignFragment = new TaskAssignFragment();

            taskAssignFragment.setArguments(bundle);


            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

            transaction.commit();


            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,

                    new TaskAssignFragment()).addToBackStack(null).commit();


            Log.i("ListView", selectedItemName);

        }

    });

TaskAssignFragment->


@Nullable

@Override

public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_task_assign, container, false);

    TextView test = view.findViewById(R.id.testerView);


    String bundlePassData = this.getArguments().getString("tagName");

    test.setText(bundlePassData);


    return view;

}

我应该试试吗?但是,如果我需要将数据作为变量,而不仅仅是在函数中,因为我会在.interfacesfragment


慕森王
浏览 106回答 4
4回答

守着星空守着你

在代码中,替换时将创建新片段。TaskAssignFragment taskAssignFragment = new TaskAssignFragment();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; taskAssignFragment.setArguments(bundle);getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new TaskAssignFragment()).addToBackStack(null).commit();使用已在其中设置参数的已创建的片段实例&nbsp; getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;taskAssignFragment).addToBackStack(null).commit();

茅侃侃

您正在创建新对象,同时替换片段。&nbsp; &nbsp; &nbsp; &nbsp; TaskAssignFragment taskAssignFragment = new TaskAssignFragment();&nbsp; &nbsp; &nbsp; &nbsp; taskAssignFragment.setArguments(bundle);&nbsp; &nbsp; &nbsp; &nbsp; getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; taskAssignFragment).addToBackStack(null).commit();试试这个

慕工程0101907

tagsView.setOnItemClickListener(new AdapterView.OnItemClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Bundle bundle = new Bundle();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TextView textView = (TextView) view.findViewById(R.id.tagName);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String selectedItemName = textView.getText().toString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bundle.putString("tagName", selectedItemName);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TaskAssignFragment taskAssignFragment = new TaskAssignFragment();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; taskAssignFragment.setArguments(bundle);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; transaction.commit();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; taskAssignFragment).addToBackStack(null).commit();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.i("ListView", selectedItemName);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });请尝试此代码,创建一个新的 TaskAssignFragment() 实例,并在没有参数的情况下传递它

慕村225694

您可以直接在片段上调用方法,例如:TaskAssignFragment taskAssignFragment = new TaskAssignFragment();// Call here your own method (from fragment)taskAssignFragment.setSelectedItemName(selectedItemName);在你的片段中:public void setSelectedItemName(String selectedItemName) {&nbsp; &nbsp; // Store data as field in class&nbsp; &nbsp; this.selectedName = selectedItemName}其中 是类中的字段。selectedName
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java