1

我正在尝试使用循环来设置 int []。在那个 int[] 中,我想为稍后使用的图片库提供可绘制对象。我希望在此活动的 onCreate() 之后立即初始化数组,但我读到您不能在 onCreate() 方法中使用循环,我必须使用线程。我设置如下,但它不起作用。我的应用程序不断崩溃。

@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 < 7; x++) {
                test[x] = rand.nextInt(7) + 1; 
            }

            for(int i = 0; i < 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();
}

链接到我的整个代码: http ://www.reddit.com/r/androiddev/comments/160l7i/need_help_with_loop_during_oncreate/

这是日志猫:

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
4

1 回答 1

1

重写
没有理由不能在内部使用循环onCreate(),但你并不特别需要你的两个。试试这个:

TextView mySelection;
int[] test = new int[7];

@Override
public void onCreate(Bundle icicle) {  
    super.onCreate(icicle);
    setContentView(R.layout.activity_gallery_demo);
    mySelection = (TextView) findViewById(R.id.mySelection);        

    for(int i = 0; i < 7; i++) {
        switch(random.nextInt(7) + 1) {
        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;
        }
    };
}

我希望这更接近您想要的,很抱歉我错过了您最初尝试做的事情...
如果您扩大图像列表,请考虑使用getIdentifier(). 这不是最快的方法,但如果使用得当,您不必输入每个R.drawable.xxxID。您可以简单地使用:

int id = getResources().getIdentifier("rs0" + i + "card", "drawable", getPackageName());
于 2013-01-05T18:14:12.843 回答