I have an Android App. And I am using reflection to call a method of CookieManager.
What I'm doing:
if (Build.VERSION.SDK_INT >= 21) {
Method method;
method = CookieManager.class.getClass().getMethod("setAcceptFileSchemeCookies", boolean.class);
method.setAccessible(true);
method.invoke(null, true);
}
Basically this method is static. You can look it up here: http://ift.tt/2kMMT8c
So I can call it everywhere, without initiating the CookieManager. (This is even the plan because you can only set this property before initiating the CookieManager)
What's the exception:
java.lang.NoSuchMethodException: setAcceptFileSchemeCookies [boolean]
at java.lang.Class.getMethod(Class.java:624)
at java.lang.Class.getMethod(Class.java:603)
at com.sample.app.CookieClass.setup(CookieClass.java:168)
at java.lang.reflect.Method.invoke(Native Method)
at com.sample.app.Application.onCreate(Application.java:111)
at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1036)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4728)
at android.app.ActivityThread.-wrap1(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1415)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
That's the question:
And now comes the funny part. When I call this in in the onCreate method of the Application class it fails with this exception. But when I call it later right in front of where I initialize the CookieManager it works. How is this possible? The reflection has to work everywhere? Why is the method not found in the onCreate, even if it is 100% there (and works later on?).
Aucun commentaire:
Enregistrer un commentaire