简体   繁体   中英

java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.FrameLayout$LayoutParams

I am using this code to move and resize the button in a relative layout...

Program crashes when I touch on the screen, what can be the reason.?

    ViewGroup relativeL;


onCreate(){
relativeL = (ViewGroup) findViewById(R.id.relativeLayout);
    relativeL.setOnTouchListener(new RelativeLayout.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            methodMove();
            return true;
        }
    });
}

public void methodMove(){
    View button = findViewById(R.id.helloButton);

    TransitionManager.beginDelayedTransition(relativeL);

    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
    layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
    relativeL.setLayoutParams(layoutParams);
    button.setLayoutParams(new RelativeLayout.LayoutParams(130,250));
}

I have tried making relative layout as just RelativeLayout type instead of ViewGroup, still didnot work. Guidance,...thank you in advance..

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="my context"
android:id="@+id/relativeLayout">

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    android:id="@+id/helloButton"/>

I'm mobile and I can't check the code but could you try changing

button.setLayoutParams(new RelativeLayout.LayoutParams(130,250));

To

button.setLayoutParams(new FrameLayout.LayoutParams(130,250));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM