0

我在一个片段中有一个 Bundle,它将一个字符串传递给另一个片段。此字符串需要在文本视图中设置文本,而我的方法不起作用。我不知道为什么,但我所有的其他字符串都通过了。

请看看我的代码,让我知道我有什么问题 - 我不明白......

从:

public void onClick(View v) {

        Bundle args = new Bundle();

        FragmentManager fm = getFragmentManager();
        final FragmentTransaction vcFT = fm.beginTransaction();
        vcFT.setCustomAnimations(R.anim.slide_in, R.anim.hyperspace_out, R.anim.hyperspace_in, R.anim.slide_out);

        switch (v.getId()) {

            case R.id.regulatoryBtn :

                String keyDiscriptionTitle = "Regulatory Guidance Library (RGL)";
                args.putString("KEY_DISCRIPTION_TITLE", keyDiscriptionTitle);

                RegulatoryDiscription rd = new RegulatoryDiscription();
                vcFT.replace(R.id.viewContainer, rd).addToBackStack(null).commit();
                rd.setArguments(args);
                break;
. . .
}

到:

public class RegulatoryDiscription extends Fragment {

    Bundle args = new Bundle();

    String DNS = "http://192.168.1.17/";
    String KEY_DISCRIPTION_TITLE = "KEY_DISCRIPTION_TITLE";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.discription_view, container, false);

        TextView title = (TextView) view.findViewById(R.id.discriptionTitle);
        String keyDiscriptionTitle = args.getString(KEY_DISCRIPTION_TITLE);
        title.setText(keyDiscriptionTitle);

        return view;
    }
 . . .
}
4

1 回答 1

4

您在RegulatoryDe​​scription Fragment 中将args 声明为一个新的Bundle。这将初始化一个完全为空的新 Bundle 对象

您需要检索您传入的现有参数。

前任。

public class RegulatoryDiscription extends Fragment {
    Bundle args;

    String DNS = "http://192.168.1.17/";
    String KEY_DISCRIPTION_TITLE = "KEY_DISCRIPTION_TITLE";
  @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.discription_view, container, false);

        args = getArguments(); //gets the args from the call to rd.setArguments(args); in your other activity

        TextView title = (TextView) view.findViewById(R.id.discriptionTitle);
        String keyDiscriptionTitle = args.getString(KEY_DISCRIPTION_TITLE);
        title.setText(keyDiscriptionTitle);

        return view;
    }
}
于 2012-06-06T20:55:47.800 回答