2012-10-25 99 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