Args Bundle 不传给 Android TextView

发布时间: 2014/8/31 7:41:54
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我有 Bundlefragment ,将字符串传递给另一个 fragment 。此字符串需要在文本中设置文本 view 和我的方法并不工作。我不知道为什么,但我所有的其他字符串穿过。

请看看我的代码,让我知道我错了-我不明白......

从:

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

解决方法 1:

您声明作为一种新的 args Bundle 在你的 RegulatoryDescription Fragment 。这将初始化一个新 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;
    }
}
官方微信
官方QQ群
31647020