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

时间:2017-01-05 18:58:53

标签: java android classcastexception runtimeexception android-collapsingtoolbarlayout

我正在尝试使用此example在我的应用中设置CollapsingToolbarLayout的“子标题”。

以下是onCreate()的{​​{1}}代码:

Profile.java

这是 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");

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>

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>

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

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

为什么我收到此错误并如何解决此问题?

请告诉我。

1 个答案:

答案 0 :(得分:2)

您没有在您的代码上显示,但我打赌您的function sentDataToSql(pos) { var numbers = $('#numbers').val(); var my_address = $('#my-address').val(); var signals=$('.signals').text(); var date=new Date(); var month=date.getMonth()+1; var day=date.getDay(); var year=date.getFullYear(); var finaldate=year+'-'+month+'-'+day; var dt = new Date(); var time = dt.getHours() + ":" + dt.getMinutes() + ":" + dt.getSeconds(); var data = new FormData(); jQuery.each(jQuery('#photo-upload')[0].files, function(i, file) { data.append('file-'+i, file); }); data.append('nums',numbers); data.append('address',my_address); data.append('type','homeless'); data.append('lat',pos['lat']); data.append('lng',pos['lng']); data.append('sigs',signals); data.append('time',time); data.append('date',date); if(my_address!=''){ $.ajax({ type:'POST', url:'../index.php', data:data, }) }else{ alert('Please locate your location first.') } 有一个header_view.xml作为根视图。

基本上发生的事情是:LinearLayout代码&#34;被替换&#34;在<include的根LinearLayout旁边,然后拨打header_view.xml,返回findViewById(R.id.toolbar_header_view),然后使用LinearLayout告诉虚拟机这是一个HeaderView,但它是一个LinearLayout。所以它崩溃了!

没有看到您没有显示的代码的最佳选择是以下之一:

  1. (HeaderView)放在<HeaderView>
  2. 的根目录下

    或者如果不可能,因为header_view.xml

    中有更多内容
    1. 更改代码以找到header_view.xml,然后在include中找到实际的HeaderView。
    2. 类似的东西:

      include

      请注意,它会两次调用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); 。一个用于包含,另一个用于其中的HeaderView