Note that there are some explanatory texts on larger screens.

plurals
  1. PODeclaring String Arrays Causing Issues
    primarykey
    data
    text
    <p>I am trying to declare a string array which I will be working with in my code. I have done this two ways, both of which I thought to be viable options, but one causes my app to crash on load without fail.</p> <p><strong>Working Code:</strong></p> <pre><code>private String[] list = {"apple", "pear"}; </code></pre> <p><em><strong>Not</em> Working Code:</strong></p> <pre><code>Resources res = getResources(); private String[] list = res.getStringArray(R.array.Fruit); </code></pre> <p>The weird thing is, the latter code was copied <em>directly</em> from the Android String Resource page. I've made sure that the array (In this case titled <code>Fruit</code>) exists, and am now kind of lost.</p> <p>The <strong>Log Cat</strong> Error I am getting is as follows:</p> <pre><code>02-25 12:38:18.015: ERROR/AndroidRuntime(605): FATAL EXCEPTION: main 02-25 12:38:18.015: ERROR/AndroidRuntime(605): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.ikiar.appname/com.ikiar.appname.Main}: java.lang.NullPointerException 02-25 12:58:52.284: ERROR/AndroidRuntime(717): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585) 02-25 12:58:52.284: ERROR/AndroidRuntime(717): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 02-25 12:58:52.284: ERROR/AndroidRuntime(717): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 02-25 12:58:52.284: ERROR/AndroidRuntime(717): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 02-25 12:58:52.284: ERROR/AndroidRuntime(717): at android.os.Handler.dispatchMessage(Handler.java:99) 02-25 12:58:52.284: ERROR/AndroidRuntime(717): at android.os.Looper.loop(Looper.java:123) 02-25 12:58:52.284: ERROR/AndroidRuntime(717): at android.app.ActivityThread.main(ActivityThread.java:4627) 02-25 12:58:52.284: ERROR/AndroidRuntime(717): at java.lang.reflect.Method.invokeNative(Native Method) 02-25 12:58:52.284: ERROR/AndroidRuntime(717): at java.lang.reflect.Method.invoke(Method.java:521) 02-25 12:58:52.284: ERROR/AndroidRuntime(717): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 02-25 12:58:52.284: ERROR/AndroidRuntime(717): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 02-25 12:58:52.284: ERROR/AndroidRuntime(717): at dalvik.system.NativeStart.main(Native Method) 02-25 12:58:52.284: ERROR/AndroidRuntime(717): Caused by: java.lang.NullPointerException 02-25 12:58:52.284: ERROR/AndroidRuntime(717): at android.content.ContextWrapper.getResources(ContextWrapper.java:80) 02-25 12:58:52.284: ERROR/AndroidRuntime(717): at com.ikiar.appname.Main.&lt;init&gt;(Main.java:24) 02-25 12:58:52.284: ERROR/AndroidRuntime(717): at java.lang.Class.newInstanceImpl(Native Method) 02-25 12:58:52.284: ERROR/AndroidRuntime(717): at java.lang.Class.newInstance(Class.java:1429) 02-25 12:58:52.284: ERROR/AndroidRuntime(717): at android.app.Instrumentation.newActivity(Instrumentation.java:1021) 02-25 12:58:52.284: ERROR/AndroidRuntime(717): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577) </code></pre> <p>Thank you for your help!</p> <p><strong>EDIT: More Log Cat information, Line of code @ location of crash added</strong></p> <p><em><strong>@ Location of Crash:</em></strong></p> <pre><code>Resources res = getResources(); </code></pre> <p><strong>Solution:</strong></p> <p>Moved the location of <code>Resources res - getResources();</code> into the <code>onCreate</code> method.</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. 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