Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is a similar problem to loading JDBC drivers. If the drivers don't exist you don't want to crash out your entire app.</p> <p>The basic principle of the solution is to use <a href="http://developer.android.com/reference/java/lang/Class.html#forName%28java.lang.String%29" rel="nofollow">Class.forName()</a></p> <p>This lets you check if the class exists and throws an exception if it doesn't.</p> <p><strong>Edit:</strong></p> <p>To be clear - the reason why this works and why this is <em>necessary</em> is that if Java is unable to execute code because it doesn't have a class then Java will treat this as a catastrophic failure (an <a href="http://developer.android.com/reference/java/lang/Error.html" rel="nofollow">Error</a> - specificity a <a href="http://developer.android.com/reference/java/lang/NoClassDefFoundError.html" rel="nofollow">NoClassDefFoundError</a>). Therefore executing any direct reference to a missing class will throw this Error. According to the <a href="http://developer.android.com/reference/java/lang/Error.html" rel="nofollow">documentation</a>:</p> <blockquote> <p>When errors are thrown, they should not be caught by application code.</p> </blockquote> <p>However when <code>Class.forName()</code> is unable to find a class (or more direct calls to a <a href="http://developer.android.com/reference/java/lang/ClassLoader.html" rel="nofollow">ClassLoader</a>) an <a href="http://developer.android.com/reference/java/lang/ClassNotFoundException.html" rel="nofollow">Exception</a> is thrown. Because this is an exception and not an Error it can be caught. And because the function takes a String as an argument, you don't need to directly reference the class (just give it's name in the string) so the check can be performed without directly referencing the class and thus without crashing out your program.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload