【问题标题】:Testing DialogFragments with Robolectric使用 Robolectric 测试 DialogFragments
【发布时间】:2012-10-15 14:04:16
【问题描述】:

我有几个关于使用 Robolectric 测试 DialogFragment 课程的问题,因为我在 Internet 上遇到了关于这个主题的完全缺乏信息。

  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 崩溃。

非常感谢有关这些主题的任何想法。

【问题讨论】:

标签: android unit-testing robolectric android-dialogfragment


【解决方案1】:

您使用的DialogFragment 错误。您不应该提交片段,而是在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");

【讨论】:

  • show() 做了几乎相同的事情 - 将 DialogFragment 添加到事务并提交它。
猜你喜欢
  • 2013-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多