在通过数据包将数据从Activity传递到Fragment

时间:2019-03-11 09:19:48

标签: java android android-fragments android-activity

通过包传递数据时丢失数据,我得到

  

试图在null对象上调用虚拟方法'java.lang.String android.os.Bundle.getString(java.lang.String)'   在 String bundlePassData = this.getArguments()。getString(“ tagName”);

这是我的代码示例。 MainActivity->

 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;
}

我应该尝试interfaces吗?但是,如果我需要将数据作为变量,而不仅仅是在函数中,因为我会在fragment的更多地方使用它。

4 个答案:

答案 0 :(得分:1)

在您的代码中,替换时正在创建新的片段。

TaskAssignFragment taskAssignFragment = new TaskAssignFragment();
            taskAssignFragment.setArguments(bundle);

getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                    new TaskAssignFragment()).addToBackStack(null).commit();

使用您已经在其中设置参数的片段实例

  getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                   taskAssignFragment).addToBackStack(null).commit();

答案 1 :(得分:0)

您可以直接在片段上调用方法,例如:

TaskAssignFragment taskAssignFragment = new TaskAssignFragment();
// Call here your own method (from fragment)
taskAssignFragment.setSelectedItemName(selectedItemName);

在您的片段中:

public void setSelectedItemName(String selectedItemName) {
    // Store data as field in class
    this.selectedName = selectedItemName
}

其中selectedName是班级中的字段。

答案 2 :(得分:0)

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,
                    taskAssignFragment).addToBackStack(null).commit();

            Log.i("ListView", selectedItemName);
        }
    });

请尝试使用此代码,您将创建一个TaskAssignFragment()的新实例,并在不带参数的情况下将其传递

答案 3 :(得分:0)

您正在替换片段时创建新对象。

        TaskAssignFragment taskAssignFragment = new TaskAssignFragment();
        taskAssignFragment.setArguments(bundle);

        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                taskAssignFragment).addToBackStack(null).commit();

尝试一下