0
private void movePlane() {
    // TODO Auto-generated method stub
    spielbereich = (FrameLayout) findViewById(R.id.spielbereich);
    int nummer = 0;
    while (nummer<spielbereich.getChildCount()) {
        ImageView muecke = (ImageView) spielbereich.getChildAt(nummer);



        if (muecke.getTag() == AIRPLANE ){


        FrameLayout.LayoutParams params = (android.widget.FrameLayout.LayoutParams) muecke.getLayoutParams();
        params.leftMargin -= 10;
        muecke.setLayoutParams(params);

        nummer ++;}
        }
    }

Hello, I want to just move the ImageView with the specific tag, but when I do it like this the app would crash. In the layout there is one permanent ImageView. The others are created within the Activity. Can anyone help with that?

user2565603
  • 31
  • 1
  • 1
  • 4

1 Answers1

0

You can't cast an an ImageView to a FrameLayout. If you want to use the margins of a FrameLayout for moving, then try wrapping each ImageView in it's own individual FrameLayout inside your spielbereich FrameLayout.

You also have no check for negative values of params.leftMargin. While this is not the cause of your main problem, it could result in unexpected behavior for a frame layout. Romain Guy (core Android Dev at Google) recommends you avoid this. Have a look at the links in the answer to this question.

Community
  • 1
  • 1
Sound Conception
  • 5,263
  • 5
  • 30
  • 47
  • What is the code calling this method? Is it a one off call, or is it looping and the view is whizzing off to infinity? – Sound Conception Sep 13 '13 at 17:25
  • but with the if statement everything works fine except the permanent ImageView also is moved. – user2565603 Sep 13 '13 at 17:27
  • Actually I wrap every ImageView in its own frameLayout: FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(muecke_breite,muecke_hoehe); params.leftMargin = breite; params.topMargin = oben; params.gravity= Gravity.TOP + Gravity.LEFT; spielbereich.addView(muecke,params); – user2565603 Sep 14 '13 at 10:44
  • Ok, it works. I had just to put "params.leftMargin -= 10;" inside the if statement the rest out of it. – user2565603 Sep 15 '13 at 16:05