【问题标题】:java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.FrameLayout$LayoutParamsjava.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams 不能转换为 android.widget.FrameLayout$LayoutParams
【发布时间】:2016-02-15 08:24:24
【问题描述】:

我正在使用此代码在相对布局中移动按钮并调整其大小...

当我触摸屏幕时程序崩溃,可能是什么原因?

    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));
}

我尝试将相对布局设置为只是相对布局类型而不是 ViewGroup,但仍然没有工作。指导,...提前谢谢您..

<?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"/>

【问题讨论】:

  • 尝试更改为 FrameLayout.LayoutParams
  • helloButton的父布局是什么
  • 它在同一个活动中......它的父布局只是相对布局......
  • 请发布您的 xml 布局
  • 如果您要放入的布局是 FrameLayout,那么您必须制作 FrameLayout.LayoutParmas,而不是 RelativeLayout.LayoutParams。

标签: java android android-layout android-fragments


【解决方案1】:

我是手机,无法检查代码,但您可以尝试更改

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

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

【讨论】:

    猜你喜欢
    • 2013-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多