2017-03-07 83 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两次,这样的逻辑得出的观点两次。这就是为什么你有异常。删除第二个。

相关问题