2017-03-07 56 views
0

我有一個問題,我使用bundle將數據從片段發送到dialogfragment,但是當我單擊片段佈局中的按鈕時會出現問題,它成功獲取數據從片段,但在我的dialogfragment它得到空值,當我調試我發現我的對話框片段運行兩次,以便爲什麼我得到空值。任何建議來解決這個問題,對不起我的英語,希望能理解我的問題。當從片段傳遞到對話框片段時,Android獲取值null

這是我的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"); 

} 

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); 
} 
+1

'showTheDialog()'是什麼?它似乎你已經在它之前調用'newFragment.show' ... – marmor

+0

謝謝,我更新我的問題,showThDialog()是用來提示我的對話框片段 –

+1

,如果你從'showTheDialog()'調用對話框片段它將再次顯示空值,並且您正在調用以顯示上一行'newFragment.show(getActivity()。getSupportFragmentManager(),「TAG」); //在這裏你打電話來顯示片段' 和'showTheDialog(); //你是否再次在這裏顯示片段? – Sony

回答

1
newFragment.show(getActivity().getSupportFragmentManager(), "TAG"); 

          showTheDialog(); 

你不必調用show兩次,這樣的邏輯得出的觀點兩次。這就是爲什麼你有異常。刪除第二個。