线性布局无法键入强制转换为关键字布局

时间:2018-02-20 13:41:49

标签: android android-linearlayout

我有一个LinnearLayout和一个关键字布局。我想将Castlayout输入到关键字布局,但它给出了异常。

java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to com.codiant.prortc.rtc.KeywordView

我想使用Linearlayout的对象引用。我发布了实现代码的活动代码。

try {
            mKeywordView = (KeywordView) getContnetView();
            mKeywordView.setListner(new KeywordView.KeyboardListner() {
                @Override
                public void onVisbile(boolean isVisibile) {
                    Toast.makeText(getApplicationContext(), "" + isVisibile, Toast.LENGTH_LONG).show();
                }
            });
        }catch (ClassCastException e)
        {
            Log.e(TAG, "onCreate: "+e);
        }

keywordChatView.java

这是KeywordChatView类

public class KeywordView extends LinearLayout {
    private  KeyboardListner mListner;

    public void setListner(KeyboardListner listner) {
        mListner = listner;
    }

    public KeywordView(Context context) {
        super(context);
    }

    public KeywordView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public KeywordView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public KeywordView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected void onLayout(boolean b, int i, int i1, int i2, int i3) {

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        Log.d("Search Layout", "Handling Keyboard Window shown");

        final int proposedheight = MeasureSpec.getSize(heightMeasureSpec);
        final int actualHeight = getHeight();

        if (actualHeight > proposedheight){
            // Keyboard is shown
            mListner.onVisbile(true);
        } else {
            // Keyboard is hidden
            mListner.onVisbile(false);
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
    public  interface KeyboardListner
    {
        public  void onVisbile(boolean isVisibile);

    }

}

这是Linearlayout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/main_layout1"
    android:weightSum="1"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.codiant.prortc.PrivateChatActivity"
    tools:showIn="@layout/private_activity_chat">

    <FrameLayout
        android:id="@+id/close_fl"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight=".3"></FrameLayout>

    <FrameLayout
        android:id="@+id/swipe_fl"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight=".7"
        android:background="#800b0b0b">

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="@dimen/dp_30"
            android:background="@color/colorAccent">

            <TextView
                android:id="@+id/title_tv"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_gravity="left"
                android:layout_marginLeft="@dimen/dp_5"
                android:gravity="center"
                android:text="Private Chat" />

            <ImageButton
                android:id="@+id/close_iv"

                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_gravity="top|right"
                android:background="@android:color/transparent"
                android:src="@drawable/ic_close" />

        </FrameLayout>

        <android.support.v7.widget.RecyclerView
            android:id="@+id/chat_rv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/dp_5"
            android:layout_marginRight="@dimen/dp_5"
            android:layout_marginTop="@dimen/dp_40" />


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:layout_marginBottom="@dimen/dp_5"
            android:layout_marginLeft="@dimen/dp_5"
            android:layout_marginRight="@dimen/dp_5"
            android:orientation="horizontal"
            android:weightSum="1">

            <EditText
                android:id="@+id/chat_et"
                android:layout_width="0dp"
                android:layout_height="@dimen/dp_40"
                android:layout_marginRight="@dimen/dp_5"
                android:layout_weight=".9"
                android:background="@drawable/circluar_border" />

            <RelativeLayout
                android:layout_width="0dp"
                android:layout_height="@dimen/dp_40"
                android:layout_alignParentRight="true"
                android:layout_weight=".1">

                <android.support.design.widget.FloatingActionButton
                    android:id="@+id/send_fab"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:scaleType="center"
                    android:src="@drawable/ic_send"
                    app:backgroundTint="@color/blue_500"
                    app:borderWidth="0dp"
                    app:elevation="6dp" />
            </RelativeLayout>

        </LinearLayout>
    </FrameLayout>
</LinearLayout>

3 个答案:

答案 0 :(得分:1)

继承的工作方向与您编写的相反。您可以将KeywordView设置为LinearLayout的变量,但反之亦然。

答案 1 :(得分:0)

您具有LinearLayout类型的对象(在XML中指定),并且无法强制转换为其子类。在XML中指定适当的布局

答案 2 :(得分:0)

因此,您需要将XML linear layout转换为KeywordView,然后才能完成任务。

<?xml version="1.0" encoding="utf-8"?>
<packagename.KeywordView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/main_layout1"
    android:weightSum="1"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.codiant.prortc.PrivateChatActivity"
    tools:showIn="@layout/private_activity_chat">
</packagename.KeywordView >