0

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?

user
  • 86,916
  • 18
  • 197
  • 190
rosu alin
  • 5,674
  • 11
  • 69
  • 150

1 Answers1

1

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);
user
  • 86,916
  • 18
  • 197
  • 190
  • The coupons was null at the the when its run. I figured this out by putting the delay to 400.But that doesn't solve anything because, it depends on your internet connection and speed. To resolved it, I'd put the "loadGutschein" call into the onClickListener that goes to the menu where the current button is. so that until i click and choose what i want form the menu, it loads the coupon in the background. And then when i use it on one of the buttons (theres not only 1 that uses loadGutschein) it is already loaded. As of the LayoutParam, doesn't matter because it becomes as big as the picture is – rosu alin Aug 13 '12 at 14:24
  • @rosualin The delay may work now but you could face the same exception if some users have a real bad data connection. – user Aug 13 '12 at 14:27
  • they have to choose a button to load a layout and then put their data in some edittexts, and only then call the post delayed. So it has a lot of time to load. I think it would TimeOut because of the server, before it would crash – rosu alin Aug 13 '12 at 14:30