【问题标题】:Android get the value null when passing from fragment to dialogfragment从片段传递到对话框片段时Android获取值null
【发布时间】:2017-07-27 10:18:51
【问题描述】:

我有一个问题,我使用 bundle 将数据从片段发送到对话框片段,但是当我单击片段布局中的按钮时出现问题,它成功地从片段中获取数据,但在我的对话框片段中它得到空值,当我调试时,我发现我的对话框片段运行了两次,这就是为什么我得到空值。任何解决这个问题的建议,对不起我的英语,希望能理解我的问题。

这是我的 onclick 将数据传递给我的对话框片段

 Button[i].setBackgroundColor(0xFF00FF00);
 Button[i].setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                               TextView test = (TextView)getView().findViewById(R.id.testproductname);
                                test.setText(pName);
                                String testproductname = test.getText().toString();

                                Bundle args = new Bundle();
                                args.putString("key", testproductname);
                                FragmentDialog newFragment = new FragmentDialog();
                                newFragment.setArguments(args);
                                newFragment.show(getActivity().getSupportFragmentManager(), "TAG");

                                showTheDialog();
                     }
             });
 }
protected void showTheDialog() {
    FragmentManager fm = getActivity().getSupportFragmentManager();
    FragmentDialog overlay = new FragmentDialog();
    overlay.show(fm, "FragmentDialog");

 }

片段对话框

   public class FragmentDialog extends DialogFragment {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = super.onCreateDialog(savedInstanceState);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        //dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.YELLOW));
        dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

        return dialog;
    }

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

        // tab slider
        sectionsPagerAdapter = new SectionsPagerAdapter(getChildFragmentManager());

        // Set up the ViewPager with the sections adapter.
        viewPager = (ViewPager) view.findViewById(R.id.modifierpager);
        viewPager.setAdapter(sectionsPagerAdapter);

        Bundle mArgs = getArguments();
        pName = mArgs.getString("key","asdasd");

        final TextView tpn = (TextView)getView().findViewById(R.id.gtestproductname);
        tpn.setText(pName);
}

【问题讨论】:

  • 什么是showTheDialog()?看来你之前已经打电话给newFragment.show了……
  • 谢谢,我更新了我的问题,showThDialog() 用于提示我的对话片段
  • 如果您从showTheDialog() 调用对话框片段,它将再次以空值显示它,并且您正在调用以在上一行newFragment.show(getActivity().getSupportFragmentManager(), "TAG"); //here you are calling to show the fragmentshowTheDialog(); //Are you showing the fragment again here? 中显示对话框
  • 一次显示 2 个对话框?
  • 尝试注释掉showTheDialog函数调用,看看是否解决问题

标签: android android-fragments android-dialogfragment


【解决方案1】:
  newFragment.show(getActivity().getSupportFragmentManager(), "TAG");

                            showTheDialog();

你不需要调用 show 两次,这个逻辑绘制了两次视图。这就是为什么你有异常。删除第二个。

【讨论】:

    猜你喜欢
    • 2017-07-21
    • 2013-07-11
    • 1970-01-01
    • 1970-01-01
    • 2020-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多