尝试实例化LayoutParams对象时Robolectric中的NPE

时间:2016-01-20 12:39:40

标签: android robolectric android-testing

我有一个Fragment实现,我想使用Robolectric框架进行测试。当我尝试运行测试时,它会失败,但异常:

java.lang.NullPointerException 
at org.robolectric.shadows.ShadowViewGroup$ShadowLayoutParams.__constructor__(ShadowViewGroup.java:141)
at android.view.ViewGroup$LayoutParams.<init>(ViewGroup.java)   
at android.view.ViewGroup$MarginLayoutParams.<init>(ViewGroup.java)     
at android.widget.FrameLayout$LayoutParams.<init>(FrameLayout.java)     
at com.lalala.ui.FullPickerFragment.onCreateView(FullPickerFragment.java:41)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517)
at android.os.Handler.handleCallback(Handler.java:739)  
at android.os.Handler.dispatchMessage(Handler.java:95)  
at org.robolectric.shadows.ShadowMessageQueue.dispatchMessage(ShadowMessageQueue.java:130)
at org.robolectric.shadows.ShadowMessageQueue.access$100(ShadowMessageQueue.java:29)
at org.robolectric.shadows.ShadowMessageQueue$1.run(ShadowMessageQueue.java:95)
at org.robolectric.util.Scheduler.runOrQueueRunnable(Scheduler.java:230)
at org.robolectric.util.Scheduler.postAtFrontOfQueue(Scheduler.java:98)
at org.robolectric.shadows.ShadowMessageQueue.enqueueMessage(ShadowMessageQueue.java:114)
at android.os.MessageQueue.enqueueMessage(MessageQueue.java)    
at android.os.Handler.enqueueMessage(Handler.java:631)  
at android.os.Handler.sendMessageAtTime(Handler.java:600)   
at android.os.Handler.sendMessageDelayed(Handler.java:570)  
at android.os.Handler.post(Handler.java:326)    
at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1519)
at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:634)
at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:613) 
at com.lalala.ui.FullPickerFragmentTest.init(FullPickerFragmentTest.java:137)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:245)
at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:185)
at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:54)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)  
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)  
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)    
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)  
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)     
at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:149)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)    
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)   
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

在此行标记为@Before注释的测试用例方法中失败:

activity.getSupportFragmentManager()
   .beginTransaction()
   .add(pickerFragment, TAG)
   .commit();
它引用的

FullPicketFragment代码放在onCreateView方法中,并且在这里:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Log.d(LOG_TAG, "onCreateView()");
        fragmentView = inflater.inflate(R.layout.bf_full_picker, container, false);
        mFragmentViewContainer = new FrameLayout(getActivity());
        ViewGroup.LayoutParams fragmentViewParams = fragmentView.getLayoutParams();
        FrameLayout.LayoutParams fragmentViewContainerParams =
                new FrameLayout.LayoutParams(fragmentViewParams);
        mFragmentViewContainer.setLayoutParams(fragmentViewContainerParams);
        mFragmentViewContainer.addView(fragmentView);
        return mFragmentViewContainer;
    }

正如您所看到的,我只是夸大了布局,并将其放入FrameLayout,其中LayoutParams属性已从充气版中复制。 Robolectric无法创建LayoutParams个对象。

我该如何解决这个问题?

我尝试使用其他构造函数创建LayoutParams的副本,但没有帮助。此外,代码本身工作正常的问题仅在于测试用例初始化。测试工作正常,直到我在onCreateView进行了这些更改。

我正在使用 Robolectric 3.0-rc3。

1 个答案:

答案 0 :(得分:1)

似乎fragmentView.getLayoutParams()在将它放入构造函数后返回null和line。你可以试试这个:

public View onCreateView(...) {
    mFragmentViewContainer = new FrameLayout(inflater.getContext());
    fragmentView = inflater.inflate(
            R.layout.bf_full_picker, 
            mFragmentViewContainer, 
            **true**);
    return mFragmentViewContainer;
}