我有單擊片段被替換的按鈕單擊偵聽器,並在下一個片段上傳遞參數。 代碼: -在roboelectric的單元測試期間獲得空指針異常
Fragment fragment = new CustomList();
Bundle args = new Bundle();
args.putString("fragment", "Custom");
args.putSerializable("productBean", productBean);
fragment.setArguments(args);
fragmentManager = getActivity().getSupportFragmentManager();
fragmentTransaction = fragmentManager
.beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.slide_in, R.anim.slide_out, R.anim.slide_enter, R.anim.slide_exit);
fragmentTransaction.replace(R.id.container_body, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
寫測試用例按鈕點擊: -
Button custom_btn = (Button) fragment.getView().findViewById(R.id.custom_btn);
custom_btn.performClick();
但在測試用例的執行,它顯示空指針異常,當上CustomList getArguments()片段。
productsBean = (ProductsBean) getArguments().getSerializable("productsBean");
它的產品在代碼中,錯誤地在這裏粘貼了錯誤的代碼。好。更多的建議 –