简体   繁体   中英

android fragment scenario crashes during snackbar creation

I've got android application. I'm writing test using fragment scenario to test my fragment. My test is simple and looks like this:

FragmentScenario.launchInContainer(ShoppingPlanningFragment.class);
...
onView(withId(R.id.buttonRemove)).perform(click());

So I'm clicking some button and when this button is clicked snackbar is make like this:

Snackbar snackbar = Snackbar.make(getActivity().findViewById(R.id.planning_mode_layout), msg, Snackbar.LENGTH_LONG);

But I'm getting following error:

androidx.test.espresso.PerformException: Error performing 'single click - At Coordinates: 809, 1949 and precision: 16, 16' on view 'with id is com.gitlab.robert.cebula.expirydate:id/buttonRemove'. at androidx.test.espresso.PerformException$Builder.build(PerformException.java:5) at androidx.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:25) at androidx.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:36) at androidx.test.espresso.ViewInteraction.waitForAndHandleInteractionResults(ViewInteraction.java:106) at androidx.test.espresso.ViewInteraction.desugaredPerform(ViewInteraction.java:43) at androidx.test.espresso.ViewInteraction.perform(ViewInteraction.java:94) at com.example.expirydate.shoppingfragment.planningfragment.ShoppingPlanningFragmentTest.clickingRemove_shouldRemovePlanningItem(ShoppingPlanningFragmentTest.java:28) at java.lang.reflect.M ethod.invoke(Native Method) 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.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.Z587FEFE304B8E3505DE89580432 C5DDCZ.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) 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.junit.runners.ParentRunner.run(ParentRunner.java:363) at androidx.test.ext.junit.runners.AndroidJUnit4.run(AndroidJUnit4.java:154) at org .junit.runners.Suite.runChild(Suite.java:128) at org.junit.runners.Suite.runChild(Suite.java:27) 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.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.Z587FEFE304B8E3505DE89580432C 5DDCZ.runner.JUnitCore.run(JUnitCore.java:137) at org.junit.runner.JUnitCore.run(JUnitCore.java:115) at androidx.test.internal.runner.TestExecutor.execute(TestExecutor.java:56) at androidx.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:395) at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2074) Caused by: android.view.InflateException: Binary XML file line #41: Binary XML file line #41: Error inflating class android.widget.Button Caused by: android.view.InflateException: Binary XML file line #41: Error inflating ZA2F2ED4F8EBC2CBB4C21A29DC40 AB61DZ android.widget.Button Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Constructor.newInstance0(Native Method) at java.lang.reflect.Constructor.newInstance(Constructor.java:334) at android.view.LayoutInflater.createView(LayoutInflater.java:647) at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58) at android.view.LayoutInflater.onCreateView(LayoutInflater.java:720) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:788) at android.view.LayoutInflater.createViewFromTag(Lay outInflater.java:730) at android.view.LayoutInflater.rInflate(LayoutInflater.java:863) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824) at android.view.LayoutInflater.inflate(LayoutInflater.java:515) at android.view.LayoutInflater.inflate(LayoutInflater.java:423) at com.google.android.material.snackbar.Snackbar.make(Snackbar.java:165) at com.example.expirydate.shoppingfragment.planningfragment.ShoppingPlanningFragment.onRemoveButtonClicked(ShoppingPlanningFragment.java:106) at com.example.expirydate.shoppingfragment.planningfragment .ShoppingPlanningFragment.lambda$uAFpgVG-8MPel7g8tIF4mCnD_tU(Unknown Source:0) at com.example.expirydate.shoppingfragment.planningfragment.-$$Lambda$ShoppingPlanningFragment$uAFpgVG-8MPel7g8tIF4mCnD_tU.onClick(Unknown Source:2) at android.view.View.performClick(View.java:6256) at android.view.View$PerformClick.run(View.java:24701) at android.os.Handler.handleCallback(Handler.java:789) at android.os.Handler.dispatchMessage(Handler.java:98) at androidx.test.espresso.base.Interrogator.loopAndInterrogate(Interrogator.java:53) at androidx.test.espresso.base.UiControllerImpl.loopUntil(UiControllerImpl.java:155) at androidx.test.espresso.base.UiControllerImpl.loopUntil(UiControll erImpl.java:149) at androidx.test.espresso.base.UiControllerImpl.injectMotionEvent(UiControllerImpl.java:53) at androidx.test.espresso.action.MotionEvents.sendUp(MotionEvents.java:122) at androidx.test.espresso.action.MotionEvents.sendUp(MotionEvents.java:117) at androidx.test.espresso.action.Tap.sendSingleTap(Tap.java:27) at androidx.test.espresso.action.Tap.access$100(Tap.java:21) at androidx.test.espresso.action.Tap$1.sendTap(Tap.java:3) at androidx.test.espresso.action.GeneralClickAction.perform(GeneralClickAction.java:23) at androidx.test.espresso.ViewInteraction$SingleExecutionViewAction.perform(ViewInteraction.java:16) at androidx.test.espresso.ViewInteraction.doPerform(ViewInteraction.Z93F725A07423FE1C889F448B33D 21F46Z:65) at androidx.test.espresso.ViewInteraction.access$100(ViewInteraction.java:15) at androidx.test.espresso.ViewInteraction$1.call(ViewInteraction.java:3) at androidx.test.espresso.ViewInteraction$1.call(ViewInteraction.java:2) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at android.os.Handler.handleCallback(Handler.java:789) at android.os.Handler.dispatchMessage(Handler.java:98) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6541) at java.lang.reflect.Method.invoke(Native Method) at Z4D236D9A2D102C5FE6AD1C50DA4BE C50Z.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) Caused by: java.lang.UnsupportedOperationException: Failed to resolve attribute at index 5: TypedValue{t=0x2/d=0x7f0300ac a=-1} at android.content.res.TypedArray.getColorStateList(TypedArray.java:538) at android.widget.TextView.(TextView.java:1212) at android.widget.Button.(Button.java:172) at android.widget.Button.(Button.java:147) at android.widget.Button.(Button.java:123) ... 42 more

If I comment snackbar creation everything works as expected. What's the problem here and how could I resolve it?

The view used to make the snackbar. This should be contained within the view hierarchy you want to display the snackbar. Generally it can be the view that was interacted with to trigger the snackbar, such as a button that was clicked, or a card that was swiped.

val contextView = findViewById<View>(R.id.context_view)

Snackbar.make(contextView, R.string.text_label, Snackbar.LENGTH_SHORT)
    .show()

The important point in the error stack.

#41: Error inflating class android.widget.Button Caused by: android.view.InflateException: Binary XML file line #41: Error inflating class android.widget.Button

Please check the xml code.

FragmentScenario tests your fragment in isolation , and it is also isolated from its host activity. It means, it doesn't launch your host activity, instead it launches an EmptyFragmentActivity . Thus when you call:

getActivity().findViewById(R.id.planning_mode_layout)

It can't find that view, because it doesn't use your host activity.

Solution: You either change the structure of your app to make the fragment independent from its host activity, or you don't test this behavior with FragmentScenario.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM