Question

I got the following onClick command for a button. But when i call it nothing appears CODE:

public void onClick(View v) {

Log.d("debug", "before loadGutschein");
                    loadGutschein();
                    ViewFlipperVino.setDisplayedChild(3);
                    layout_number = 3;
                    Log.d("debug", "after loadGutschein");
                    bottomAudioLogosLayout.postDelayed(new Runnable() {

                        DisplayMetrics metrics = getActivity().getResources().getDisplayMetrics();
                        int width = metrics.widthPixels;


                        @Override
                        public void run() {
                            Log.d("debug", "couponsout=" + coupons);

                            if (coupons != null) {
                                int coupSize = coupons.size();
                                final int itemWidth = (width / coupSize) - 5;
                                for (int i = 0; i < coupSize; i++) {
                                    Coupon coupon = coupons.get(i);
                                    if (coupon.getImage() != null) {
                                        final CheckBox cb = new CheckBox(getActivity());
                                        final ImageView iv = new ImageView(getActivity());
                                        iv.setScaleType(ScaleType.CENTER_INSIDE);
                                        LayoutParams linearLayoutParams = new LayoutParams();
                                        iv.setLayoutParams(linearLayoutParams);
                                        cb.setLayoutParams(linearLayoutParams);
                                        bottomAudioLogosLayout.addView(iv);
                                        bottomAudioLogosLayout.addView(cb);
                                        ImageUtil.loadImage(coupon.getImage(), iv, -1, -1, "", false);
                                    }
                                }
                            }

                        }
                    }, 200);
}

============LoadGutschein Method:

   private void loadGutschein() {
    ServiceProxy.createWebServiceTask(getActivity(), new RemoteCallListener() {

        @Override
        public void onRemoteCallError(String response) {
            Log.e("error", "onRemoteCallError is ==>" + response);
        }

        @Override
        public void onRemoteCallComplete(Object response) {
            Log.d("debug", "response is = " + response + "\t" + response.getClass());
            coupons = (Coupons) response;
            Log.d("debug", "coupons = " + coupons);


        }

        @Override
        public void onNoInternetError() {
            Log.e("error", "onNoInternetError");
        }

        @Override
        public void onNoAccess() {
            Log.e("error", "onNoAccess");
        }
    }, true, true).invokeGetCoupons();
}

=========MY LOGCAT LOG:

08-13 14:36:11.382: D/debug(17576): gutscheinName= e
08-13 14:36:11.382: D/debug(17576): gutscheinHN= d
08-13 14:36:11.382: D/debug(17576): gutscheinCash= 50
08-13 14:36:11.382: D/debug(17576): before loadGutschein
08-13 14:36:11.445: D/debug(17576): after loadGutschein
08-13 14:36:11.515: I/WEB-SERVER-CLIENT GET :(17576):     http://developer.weinco.de/api/v1/getCoupons?
08-13 14:36:11.648: D/debug(17576): couponsout=null
08-13 14:36:11.742: D/debug(17576): response is = [Coupon [Name=Rabatt, Code=321, GueltigBis=2014-09-01, Typ=absolut, Wert=10, GueltigAb=2011-09-01, Gueltig=alle]] class com.weinco.webservice.entity.Coupons
08-13 14:36:11.742: D/debug(17576): coupons = [Coupon [Name=Rabatt, Code=321, GueltigBis=2014-09-01, Typ=absolut, Wert=10, GueltigAb=2011-09-01, Gueltig=alle]]

I don't get why the coupons value in the postDelayed (couponsout in logcat) is null, if coupons from the webservice takes the valures of the response. Any ideea how i could work this out?

Was it helpful?

Solution

I don't get why the coupons value in the postDelayed (couponsout in logcat) is null, if coupons from the webservice takes the valures of the response.

I think the problem appears because of the way you use the data returned from the webservice. In the onClick callback you start the task of getting the data(and set the results in the callback for the task complete event onRemoteCallComplete) and then schedule a Runnable to be run after 200ms(maybe you could explain why you post the Runnable after exactly 200ms). What if the task takes more then 200 ms, wouldn't posting a Runnable that depends on the coupons variable fail because the task to get new data hasn't yet completed?

Probably you don't initialize coupons so there is a good chance of it being null at the time when the Runnable is run. The code in the Runnable's run method should be run only when you are certain that the process of getting data from the webservice is finished(the onRemoteCallComplete seems like a good place but I don't know your full code).

Also, you should give some sizes to your LayoutParams and not just instantiate it, for example:

LayoutParams linearLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top