我想将参数从活动传递到片段。
这是来自活动的代码:

public class MatchActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_match);
        Bundle bundle = new Bundle();
        bundle.putString("edttext", "From Activity");
        MarqueFragment fragobj = new MarqueFragment();
        fragobj.setArguments(bundle);

    }

}


这是片段中的代码:

public class MarqueFragment extends Fragment {

    @Override
    public void onCreate ( Bundle savedInstanceState ) {
        super.onCreate(savedInstanceState);
    }
    @Override
    public View onCreateView ( LayoutInflater inflater, ViewGroup container,   Bundle savedInstanceState ) {
        View convertView=inflater.inflate(R.layout.fragment_marque, container, false);


            if (getArguments() != null) {
                String mParam1 = getArguments().getString("edttext");
                System.out.println("got this from MatchActivity "+mParam1);
            }else{
                System.out.println("got nothing");
            }
}


我没有任何争论,我不知道为什么。
注意:不会出现错误,输出将“无所获”。

最佳答案

将您的代码更改为此

Bundle bundle = new Bundle();
bundle.putString(edttext", "From Activity");
MarqueFragment fragobj = new MarqueFragment();
fragobj.setArguments(bundle);
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_marque, fragobj);
transaction.commit();

10-08 18:12