每次调用OnCreate时运行一个线程

时间:2015-03-10 01:14:08

标签: android

我每次创建活动时都试图运行以下线程。它是第一次工作,但如果我按下后退按钮并尝试再次导航到该活动,它总是第二次崩溃。如何确保每次都成功运行此线程?

new Thread(new Runnable() {

                public void run() {

                    for (int i = 0; i < 5; i++) {

                        try {
                            URL url = new URL(priceRangeLargeImageString[i]);
                            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                            connection.setDoInput(true);
                            connection.connect();
                            InputStream input = connection.getInputStream();
                            Bitmap bitmap = BitmapFactory.decodeStream(input);
                            largeImage.add(bitmap);

                        } catch (IOException e) {
                            e.printStackTrace();
                            Log.e("Exception", e.getLocalizedMessage());
                        }
                    }
                    latch.countDown();
                }
            }).start();

                try {
                    latch.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                addtoView();

我的线程正在填充我在这里放入自定义适配器的ArrayList:

public View getView(int position,View convertView,ViewGroup parent){

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.forlist, parent, false);

    }

  //  parent.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);

    TextView address = (TextView) convertView.findViewById(R.id.Address);
    address.setText(flowername.get(position));

    TextView price = (TextView) convertView.findViewById(R.id.price);

    price.setText("$" + flowerprice.get(position), TextView.BufferType.SPANNABLE);
    Spannable spannable = (Spannable) price.getText();
    spannable.setSpan(STRIKE_THROUGH_SPAN, 0, price.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    TextView discount = (TextView) convertView.findViewById(R.id.discountPrice);

   discountedPrice = Double.parseDouble(flowerprice.get(position)) / 1.15;
   String priceString = new DecimalFormat("$##.00").format(discountedPrice);
    discount.setText(priceString);

    TextView size = (TextView) convertView.findViewById(R.id.sizes);
    size.setText(flowersize.get(position));

    final ImageView image = (ImageView) convertView.findViewById(R.id.flowerImage);
    image.setImageBitmap(flowerimage.get(position));

    return convertView;
}

我的logcat如下:

03-09 21:33:01.540 18890-18890 / com.example.andrewberkowitz.bae W / dalvikvm:threadid = 1:线程退出未捕获的异常(group = 0x41862da0) 03-09 21:33:01.550 18890-18890 / com.example.andrewberkowitz.bae E / AndroidRuntime:FATAL EXCEPTION:main     过程:com.example.andrewberkowitz.bae,PID:18890     java.lang.IndexOutOfBoundsException:索引0无效,大小为0             at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)             at java.util.ArrayList.get(ArrayList.java:308)             在com.example.andrewberkowitz.bae.FlowerShopAdapter.getView(FlowerShopAdapter.java:106)             在android.widget.AbsListView.obtainView(AbsListView.java:2780)             在android.widget.ListView.measureHeightOfChildren(ListView.java:1274)             在android.widget.ListView.onMeasure(ListView.java:1186)             在android.view.View.measure(View.java:17594)             在android.widget.RelativeLayout.measureChildHorizo​​ntal(RelativeLayout.java:719)             在android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455)             在android.view.View.measure(View.java:17594)             在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5398)             在android.widget.FrameLayout.onMeasure(FrameLayout.java:310)             在android.view.View.measure(View.java:17594)             在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5398)             在android.support.v7.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:453)             在android.view.View.measure(View.java:17594)             在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5398)             在android.widget.FrameLayout.onMeasure(FrameLayout.java:310)             在android.view.View.measure(View.java:17594)             在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5398)             在android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410)             在android.widget.LinearLayout.measureVertical(LinearLayout.java:695)             在android.widget.LinearLayout.onMeasure(LinearLayout.java:588)             在android.view.View.measure(View.java:17594)             在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5398)             在android.widget.FrameLayout.onMeasure(FrameLayout.java:310)             在com.android.internal.policy.impl.PhoneWindow $ DecorView.onMeasure(PhoneWindow.java:2588)             在android.view.View.measure(View.java:17594)             在android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2308)             在android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1409)             在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1610)             在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1267)             在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:6638)             在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:813)             在android.view.Choreographer.doCallbacks(Choreographer.java:613)             在android.view.Choreographer.doFrame(Choreographer.java:583)             在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:799)             在android.os.Handler.handleCallback(Handler.java:733)             在android.os.Handler.dispatchMessage(Handler.java:95)             在android.os.Looper.loop(Looper.java:146)             在android.app.ActivityThread.main(ActivityThread.java:5635)             at java.lang.reflect.Method.invokeNative(Native Method)             在java.lang.reflect.Method.invoke(Method.java:515)             在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1291)             在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)             at dalvik.system.NativeStart.main(Native Method) 03-09 21:33:01.830 18890-19449 / com.example.andrewberkowitz.bae D / dalvikvm:GC_FOR_ALLOC释放792K,31%免费19230K / 27848K,暂停25ms,总计25ms 03-09 21:33:02.201 18890-19449 / com.example.andrewberkowitz.bae D / dalvikvm:GC_FOR_ALLOC释放83K,25%免费21018K / 27848K,暂停26ms,总计26ms 03-09 21:33:02.381 18890-19449 / com.example.andrewberkowitz.bae D / dalvikvm:GC_FOR_ALLOC释放30K,22%免费21923K / 27848K,暂停22ms,总计22ms

1 个答案:

答案 0 :(得分:0)

如果您导航回已经创建的活动,则不会调用onCreate。

尝试将代码移动到onResume():

@Override
protected void onResume(){
    super.onResume();
new Thread(new Runnable() {

            public void run() {

                for (int i = 0; i < 5; i++) {

                    try {
                        URL url = new URL(priceRangeLargeImageString[i]);
                        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                        connection.setDoInput(true);
                        connection.connect();
                        InputStream input = connection.getInputStream();
                        Bitmap bitmap = BitmapFactory.decodeStream(input);
                        largeImage.add(bitmap);

                    } catch (IOException e) {
                        e.printStackTrace();
                        Log.e("Exception", e.getLocalizedMessage());
                    }
                }
                latch.countDown();
            }
        }).start();
}