Reputation: 439
I am having one LinnearLayout and one Keyword layout. I want to type cast Linearlayout to Keyword layout but It gives exception.
java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to com.codiant.prortc.rtc.KeywordView
I want to use the object reference of Linearlayout. I am posting the code of activity where I implement code .
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
This is KeywordChatView class
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);
}
}
this is 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>
Upvotes: 1
Views: 309
Reputation: 301
Inheritance works in the opposite direction than you have written. You can set KeywordView
to the variable of LinearLayout
, but not vice-versa.
Upvotes: 1
Reputation: 38439
So you need to convert your XML linear layout
to your KeywordView
then you can do what you task.
<?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 >
Upvotes: 0
Reputation: 12367
You have object of type of LinearLayout ( as specified in XML ) and it can not be cast to its Subclass. Specify proper layout in your XML
Upvotes: 0