Using a loop while in onCreate()
<p>I am trying to use a loop to set up an int[]. Within that int[], I want to have drawables for an Image Gallery used later. 
I wanted the array to be initialized right after onCreate() for this activity, but I read that you cannot use loops within the onCreate() method and I would have to use a thread. I set it up as follows, but it will not work. My app keeps crashing. </p>

<pre><code>@Override
public void onCreate(Bundle icicle) {  

    super.onCreate(icicle);

    setContentView(R.layout.activity_gallery_demo);

    mySelection = (TextView) findViewById(R.id.mySelection);        

    Runnable myRunnable = new Runnable() {

        @Override
        public void run() {

            for(int x = 0; x &lt; 7; x++) {
                test[x] = rand.nextInt(7) + 1; 
            }

            for(int i = 0; i &lt; 7; i++) {
                switch(test[i]) {
                case 1: 
                    test[i] = R.drawable.rs01card;
                    break;
                case 2: 
                    test[i] = R.drawable.rs02card;
                    break;
                case 3: 
                    test[i] = R.drawable.rs03card;
                    break;
                case 4: 
                    test[i] = R.drawable.rs04card;
                    break;
                case 5:
                    test[i] = R.drawable.rs05card;
                    break;
                case 6: 
                    test[i] = R.drawable.rs06card;
                    break;
                case 7: 
                    test[i] = R.drawable.rs07card;
                    break;
                }
            }
        }
    };

    Thread myThread = new Thread(myRunnable);
    myThread.start();
}
</code></pre>

<p>Link to my entire code: 
<a href="http://www.reddit.com/r/androiddev/comments/160l7i/need_help_with_loop_during_oncreate/" rel="nofollow">http://www.reddit.com/r/androiddev/comments/160l7i/need_help_with_loop_during_oncreate/</a></p>

<p>Here is the logcat: </p>

<pre><code>01-05 12:58:15.274: I/ActivityStack(205): START {act=com.levingston.minisapp.GalleryDemo cmp=com.levingston.minisapp/.GalleryDemo} from pid 7744
01-05 12:58:15.274: V/ActivityStack(205): Prepare open transition: starting ActivityRecord{41b4efe8 com.levingston.minisapp/.GalleryDemo}
01-05 12:58:15.274: V/ActivityStack(205): Prepare open transition: prev=ActivityRecord{41bcd2c0 com.levingston.minisapp/.MainActivity}
01-05 12:58:15.289: D/AndroidRuntime(7744): Shutting down VM
01-05 12:58:15.289: W/dalvikvm(7744): threadid=1: thread exiting with uncaught exception (group=0x40a811f8)
01-05 12:58:15.289: E/AndroidRuntime(7744): FATAL EXCEPTION: main
01-05 12:58:15.289: E/AndroidRuntime(7744): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.levingston.minisapp/com.levingston.minisapp.GalleryDemo}: java.lang.NullPointerException
01-05 12:58:15.289: E/AndroidRuntime(7744):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2007)
01-05 12:58:15.289: E/AndroidRuntime(7744):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2032)
01-05 12:58:15.289: E/AndroidRuntime(7744):     at android.app.ActivityThread.access$600(ActivityThread.java:126)
01-05 12:58:15.289: E/AndroidRuntime(7744):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1167)
01-05 12:58:15.289: E/AndroidRuntime(7744):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-05 12:58:15.289: E/AndroidRuntime(7744):     at android.os.Looper.loop(Looper.java:137)
01-05 12:58:15.289: E/AndroidRuntime(7744):     at android.app.ActivityThread.main(ActivityThread.java:4487)
01-05 12:58:15.289: E/AndroidRuntime(7744):     at java.lang.reflect.Method.invokeNative(Native Method)
01-05 12:58:15.289: E/AndroidRuntime(7744):     at java.lang.reflect.Method.invoke(Method.java:511)
01-05 12:58:15.289: E/AndroidRuntime(7744):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-05 12:58:15.289: E/AndroidRuntime(7744):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-05 12:58:15.289: E/AndroidRuntime(7744):     at dalvik.system.NativeStart.main(Native Method)
01-05 12:58:15.289: E/AndroidRuntime(7744): Caused by: java.lang.NullPointerException
01-05 12:58:15.289: E/AndroidRuntime(7744):     at com.levingston.minisapp.GalleryDemo.onCreate(GalleryDemo.java:53)
01-05 12:58:15.289: E/AndroidRuntime(7744):     at android.app.Activity.performCreate(Activity.java:4635)
01-05 12:58:15.289: E/AndroidRuntime(7744):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
01-05 12:58:15.289: E/AndroidRuntime(7744):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1971)
01-05 12:58:15.289: E/AndroidRuntime(7744):     ... 11 more
01-05 12:58:15.297: W/ActivityManager(205):   Force finishing activity com.levingston.minisapp/.GalleryDemo
01-05 12:58:15.297: V/ActivityStack(205): Prepare close transition: finishing ActivityRecord{41b4efe8 com.levingston.minisapp/.GalleryDemo}
01-05 12:58:15.305: W/ActivityManager(205):   Force finishing activity com.levingston.minisapp/.MainActivity
</code></pre>
