0

I want to extend my previous question.

If in my code there is a method provided by future API, also if I check the version I have the following warning in LogCat

04-24 09:30:12.565: I/dalvikvm(471): Could not find method android.view.ViewTreeObserver.removeOnGlobalLayoutListener, referenced from method com.my.project.ActivityHome.removeLayoutListenerPost16
04-24 09:30:12.565: W/dalvikvm(471): VFY: unable to resolve virtual method 2950: Landroid/view/ViewTreeObserver;.removeOnGlobalLayoutListener (Landroid/view/ViewTreeObserver$OnGlobalLayoutListener;)V

In my case (see linked question) I can resolve using reflection

try {
    Method m = ViewTreeObserver.class.getMethod("removeOnGlobalLayoutListener", OnGlobalLayoutListener.class);
    m.invoke(observer, listener);   
} catch (NoSuchMethodException e) {
    e.printStackTrace();
} catch (IllegalArgumentException e) {
    e.printStackTrace();
} catch (IllegalAccessException e) {
    e.printStackTrace();
} catch (InvocationTargetException e) {
    e.printStackTrace();
}

My question is: it is better to use reflection and avoid warning or I it is better to ignore warning avoiding reflection?

My reflection code doesn't catch exceptions, but I am not sure it is correct, is it?

Community
  • 1
  • 1
crbin1
  • 2,219
  • 3
  • 22
  • 29
  • 1
    I will go with reflection . What does "My reflection code doesn't catch exceptions, but I am not sure it is correct, is it?" mean ? You are catching the execptions but you are not managing it – Blackbelt Apr 24 '13 at 13:33
  • It means that it is the first time I use reflection and I don't find an example for method "removeOnGlobalLayoutListener", so I try to understand how it works and after a lot of attempts that catched exceptions I find the posted code that "seems" to work. I was asking if the code is correct – crbin1 Apr 24 '13 at 13:38

1 Answers1

0

The warning is just a warning, it does not cause any problem during the execution.

Testing the presence of a method using reflection and exception handling is costly and should be avoided when possible.

You should the Build.VERSION.SDK_INT constant to check on which platform you are actually running

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
   // Do something using Gingerbread API
else {
   // Workaround for earlier version of android
}
nicopico
  • 3,606
  • 1
  • 28
  • 30
  • Yes, I check version, you can see complete code here http://stackoverflow.com/questions/16189525/ongloballayoutlistener-deprecation-and-compatibility. Without check I have naturally an error with check I have only a warning. I am trying to understand If the warning is more or less costly than reflection – crbin1 Apr 24 '13 at 13:46
  • I edited my answer to better answer your question. As a matter of fact, ActionBarSherlock (a well known android library) displays the same warnings, which was deemed acceptable. See https://github.com/JakeWharton/ActionBarSherlock/issues/366 for reference. – nicopico Apr 24 '13 at 13:52