onViewCreated with compatibility library

2019-05-07 13:09发布

问题:

In an Android application I have a fragment implemented that overrides onViewCreated to set up some OnClickListeners once the view is there.

This all works fine when I implement. However as soon as I add the compatibility library v4 r3 it seems that the method is not called at all.

For now I am migrating my setup into onResume but that is really not ideal. So here are my questions:

  • Is this a bug in the compatibility library?

  • Is there a better workaround?

After some more digging and trying different things I got this stacktrace, which lets me believe it is indeed a bug in the compatibility library.

10-07 14:25:11.130: ERROR/AndroidRuntime(2964): FATAL EXCEPTION: main
        java.lang.NoSuchMethodError: android.support.v4.app.Fragment.onViewCreated
        at roboguice.fragment.RoboFragment.onViewCreated(RoboFragment.java:18)

But even more weirdly. Looking at the compatibility library source that method is actually there but it is empty.

回答1:

After much back and forth and debugging I found the problem. It turns out I was using an old version of the compatibility library in my project. I have now updated my project and things work fine. For reference the needed change is to the compatibility library dependency in the maven pom file to be like this

        <dependency>
            <groupId>android.support</groupId>
            <artifactId>compatibility-v4</artifactId>
            <version>r3</version>
        </dependency>

with the library deployed to your Maven repo with the Maven Android SDK Deployer. I have also updated the roboguice wiki and the linked gist on my github account.