简体   繁体   English

尝试设置CollapsingToolbarLayout的字幕时获取“ java.lang.ClassCastException”

[英]Getting 'java.lang.ClassCastException' while trying to set subtitle of a CollapsingToolbarLayout

I'm trying to set 'Sub-title' of a CollapsingToolbarLayout in my app using this example here. 我正在尝试使用此处的示例在我的应用程序中设置CollapsingToolbarLayout “字幕”。

Here's the code from onCreate() of Profile.java : 这是Profile.java onCreate()中的代码:

    CollapsingToolbarLayout collapsingToolbarLayout;
    Toolbar toolbar;
    HeaderView toolbarHeaderView;
    HeaderView floatHeaderView;

    collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.collapse_toolbar);

    // error on the line below
    toolbarHeaderView = (HeaderView) findViewById(R.id.toolbar_header_view);
    floatHeaderView = (HeaderView) findViewById(R.id.float_header_view);

    toolbarHeaderView.bindTo("title", "subtitle");
    floatHeaderView.bindTo("title", "subtitle");

Here's activity_main.xml : 这是activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    android:id="@+id/coordinatorLayout"
    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:fitsSystemWindows="true"
    tools:context="com.abc.zzz.Profile">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_width="match_parent"
        android:layout_height="256dp"
        android:theme="@style/AppTheme.AppBarOverlay"
        android:fitsSystemWindows="true">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapse_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:contentScrim="@color/colorPrimary"
            android:fitsSystemWindows="true"
            app:popupTheme="@style/AppTheme.PopupOverlay">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="parallax">

                <include
                    android:id="@+id/toolbar_header_view"
                    layout="@layout/header_view"
                    android:layout_height="wrap_content"
                    android:layout_width="match_parent"
                    android:layout_marginRight="@dimen/header_view_end_margin_right"
                    android:layout_marginEnd="@dimen/header_view_end_margin_right"
                    android:visibility="gone"
                    />

            </android.support.v7.widget.Toolbar>

        </android.support.design.widget.CollapsingToolbarLayout>

    </android.support.design.widget.AppBarLayout>

    <include
        android:id="@+id/float_header_view"
        layout="@layout/header_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="com.abc.zzz.ViewBehavior"/>

</android.support.design.widget.CoordinatorLayout>

Here's header_view.xml : 这是header_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <!-- Title -->
    <TextView
        android:id="@+id/header_view_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@android:color/white"
        android:textSize="18sp"
        />

    <!-- Subtitle -->
    <TextView
        android:id="@+id/header_view_sub_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@android:color/white"
        android:textSize="16sp"
        />

</LinearLayout>

Here's HeaderView.java : 这是HeaderView.java

public class HeaderView extends LinearLayout {

    TextView title;

    TextView subTitle;

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

    public HeaderView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public HeaderView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public HeaderView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }


    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        title = (TextView) findViewById(R.id.header_view_title);
        subTitle = (TextView) findViewById(R.id.header_view_sub_title);
    }

    public void bindTo(String title) {
        bindTo(title, "");
    }

    public void bindTo(String title, String subTitle) {
        hideOrSetText(this.title, title);
        hideOrSetText(this.subTitle, subTitle);
    }

    private void hideOrSetText(TextView tv, String text) {
        if (text == null || text.equals(""))
            tv.setVisibility(GONE);
        else
            tv.setText(text);
    }
}

Here's ViewBehavior.java : 这是ViewBehavior.java

public class ViewBehavior extends CoordinatorLayout.Behavior<HeaderView> {

    private Context mContext;

    private int mStartMarginLeft;
    private int mEndMargintLeft;
    private int mMarginRight;
    private int mStartMarginBottom;
    private boolean isHide;

    public ViewBehavior(Context context, AttributeSet attrs) {
        mContext = context;
    }

    @Override
    public boolean layoutDependsOn(CoordinatorLayout parent, HeaderView child, View dependency) {
        return dependency instanceof AppBarLayout;
    }

    @Override
    public boolean onDependentViewChanged(CoordinatorLayout parent, HeaderView child, View dependency) {
        shouldInitProperties(child, dependency);

        int maxScroll = ((AppBarLayout) dependency).getTotalScrollRange();
        float percentage = Math.abs(dependency.getY()) / (float) maxScroll;

        float childPosition = dependency.getHeight()
                + dependency.getY()
                - child.getHeight()
                - (getToolbarHeight() - child.getHeight()) * percentage / 2;


        childPosition = childPosition - mStartMarginBottom * (1f - percentage);

        CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams) child.getLayoutParams();
        lp.leftMargin = (int) (percentage * mEndMargintLeft) + mStartMarginLeft;
        lp.rightMargin = mMarginRight;
        child.setLayoutParams(lp);

        child.setY(childPosition);

        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            if (isHide && percentage < 1) {
                child.setVisibility(View.VISIBLE);
                isHide = false;
            } else if (!isHide && percentage == 1) {
                child.setVisibility(View.GONE);
                isHide = true;
            }
        }
        return true;
    }

    private void shouldInitProperties(HeaderView child, View dependency) {

        if (mStartMarginLeft == 0)
            mStartMarginLeft = mContext.getResources().getDimensionPixelOffset(R.dimen.header_view_start_margin_left);

        if (mEndMargintLeft == 0)
            mEndMargintLeft = mContext.getResources().getDimensionPixelOffset(R.dimen.header_view_end_margin_left);

        if (mStartMarginBottom == 0)
            mStartMarginBottom = mContext.getResources().getDimensionPixelOffset(R.dimen.header_view_start_margin_bottom);

        if (mMarginRight == 0)
            mMarginRight = mContext.getResources().getDimensionPixelOffset(R.dimen.header_view_end_margin_right);

    }


    public int getToolbarHeight() {
        int result = 0;
        TypedValue tv = new TypedValue();
        if (mContext.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
            result = TypedValue.complexToDimensionPixelSize(tv.data, mContext.getResources().getDisplayMetrics());
        }
        return result;
    }

}

The problem is that I'm getting this error: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.abc.zzz/com.abc.zzz.Profile}: java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to com.abc.zzz.HeaderView on the line specified above. 问题是我遇到此错误: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.abc.zzz/com.abc.zzz.Profile}: java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to com.abc.zzz.HeaderView上面指定的行java.lang.RuntimeException: Unable to start activity ComponentInfo{com.abc.zzz/com.abc.zzz.Profile}: java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to com.abc.zzz.HeaderView

Why am I getting this error and how to resolve it? 为什么会出现此错误以及如何解决?

Please let me know. 请告诉我。

You didn't show on your code but I bet that your header_view.xml have a LinearLayout as the root view. 您没有在代码上显示,但我敢打赌,您的header_view.xml具有LinearLayout作为根视图。

So basically what happens is: the <include code "gets replaced" by the LinearLayout at the root of header_view.xml and then you call findViewById(R.id.toolbar_header_view) which returns that LinearLayout and then with the (HeaderView) you're telling the VM this is a HeaderView, but it's, it's a LinearLayout. 所以基本上发生了什么: <include代码“被header_view.xml的根目录下的LinearLayout替换”,然后调用findViewById(R.id.toolbar_header_view) ,后者返回该LinearLayout ,然后返回(HeaderView)告诉VM这是HeaderView,但这是LinearLayout。 So it crashes! 所以它崩溃了!

The best option without seeing piece of code you didn't show it is one of the following: 没有看到您未显示的代码的最佳选择是以下之一:

  1. put <HeaderView> at the root of header_view.xml , <HeaderView>放在header_view.xml的根目录中,

or if that is not possible because there's more stuff inside header_view.xml 或者这是不可能的,因为header_view.xml还有更多内容

  1. change your code to find the include and then inside the include, to find the actual HeaderView. 更改代码以查找include ,然后在包含内部查找实际的HeaderView。

Something like: 就像是:

toolbarHeaderView = (HeaderView) findViewById(R.id.toolbar_header_view).findViewById(R.id.header_view_id);
floatHeaderView = (HeaderView) findViewById(R.id.float_header_view).findViewById(R.id.header_view_id);

note that it calls findViewById two times. 请注意,它两次调用findViewById One for the include and another for the HeaderView inside it 一个用于包含,另一个用于其中的HeaderView

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试访问列表中的元素时获取java.lang.ClassCastException - Getting java.lang.ClassCastException while trying to access element in the list 尝试开发Android应用程序时获取java.lang.ClassCastException - Getting java.lang.ClassCastException while trying to develop an android application 得到 java.lang.ClassCastException - getting java.lang.ClassCastException 尝试在翡翠中打印容器ID时出现java.lang.ClassCastException - java.lang.ClassCastException while trying to print container id in jade 在我的应用程序中获取java.lang.ClassCastException - Getting java.lang.ClassCastException in my app 膨胀布局时的 java.lang.ClassCastException - java.lang.ClassCastException while inflating layout java.lang.ClassCastException - java.lang.ClassCastException 尝试在Java中编写优先级队列,但获得“线程“ main”中的异常” java.lang.ClassCastException” - Trying to write priority queue in Java but getting “Exception in thread ”main“ java.lang.ClassCastException” 从数据库获取值时如何修复java.lang.ClassCastException:java.util.ArrayList - how to fix java.lang.ClassCastException: java.util.ArrayList while getting values from DB java.lang.classcastException尝试将Blob转换为bufferedImage变量 - Java.lang.classcastexception trying to convert blob to bufferedImage variable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM