2012-10-25 60 views
13

我有几个关于使用Robolectric测试DialogFragment类的问题,因为我在Internet上完全没有关于此主题的信息。使用Robolectric测试DialogFragments

  1. 什么是正确的参数传递给onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
  2. 我有麻烦参数传递给DialogFragment,我使用以下方法:

    activity = new Activity(); 
    dialog = new DialogFragment(); 
    Bundle bundle = new Bundle(); 
    dialog.setArguments(bundle); 
    FragmentManager fm = activity.getSupportFragmentManager(); 
    FragmentTransaction ft = fm.beginTransaction(); 
    ft.add(dialog, "fragment"); 
    ft.commit(); 
    

每当代码试图访问其与NullPointerException崩溃的论点。

请高度评价这些主题的任何想法。

+0

您可以包含DialogFragment类的代码吗?有没有理由不使用DialogFragment#show()?该文档显示了如何扩展DialogFragment以创建自己的自定义对话框:http://developer.android.com/reference/android/app/DialogFragment.html – user697495

+0

您是否解决了这个问题? –

+0

@Egor,你解决了这个问题吗? –

回答

0

您正在使用DialogFragment错误。您不应该提交该片段,但请拨打上的show

activity = new Activity(); 
dialog = new DialogFragment(); 
Bundle bundle = new Bundle(); 
dialog.setArguments(bundle); 
FragmentManager fm = activity.getSupportFragmentManager(); 
FragmentTransaction ft = fm.beginTransaction(); 
dialog.show(ft, "fragment"); 
+0

show()做几乎相同的事情 - 将DialogFragment添加到事务并提交它。 – Egor