簡體   English   中英

從片段傳遞到對話框片段時,Android獲得值null

[英]Android get the value null when passing from fragment to dialogfragment

我有一個問題,我使用包將數據從片段發送到dialogfragment,但是當我單擊片段布局中的按鈕時,成功從片段中獲取數據卻在我的dialogfragment中獲取空值是有問題的調試我發現我的對話框片段運行了兩次,所以為什么我得到了空值。 任何解決這個問題的建議,對不起我的英語,希望能理解我的問題。

這是我將數據傳遞到我的對話框片段的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);
}
  newFragment.show(getActivity().getSupportFragmentManager(), "TAG");

                            showTheDialog();

您無需調用show兩次,此邏輯兩次繪制視圖。 這就是為什么您有異常。 刪除第二個。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM