View子类调用findViewById()方法报cannot cast 错误

阿不阿 2014-04-11 01:11:27
BorderScrollView 继承 ScrollView类。
BorderScrollView 创建一个接口供其他类implements:OnBorderListener。
代码如下:

/**
* 重定义ScrollView
* 定义onTop()、onBottom()判断
* 应用场景:滑动到最下方正动加载等
*/
package net.iwetao.init;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ScrollView;

/**
* BorderScrollView
* <ul>
* <li>onTop and onBottom response ScrollView</li>
* <li>you can {@link #setOnBorderListener(OnBorderListener)} to set your top and bottom response</li>
* </ul>
*
* @author trinea@trinea.cn 2013-5-21
*/
public class BorderScrollView extends ScrollView {

private OnBorderListener onBorderListener;
private View contentView;

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

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

public BorderScrollView(Context context, AttributeSet attrs, int defStyle){
super(context, attrs, defStyle);
}

@Override
protected void onScrollChanged(int x, int y, int oldx, int oldy) {
super.onScrollChanged(x, y, oldx, oldy);
doOnBorderListener();
}

public void setOnBorderListener(final OnBorderListener onBorderListener) {
this.onBorderListener = onBorderListener;
if (onBorderListener == null) {
return;
}

if (contentView == null) {
contentView = getChildAt(0);
}
}

/**
* OnBorderListener, Called when scroll to top or bottom
*
* @author <a href="http://www.trinea.cn" target="_blank">Trinea</a> 2013-5-22
*/
public static interface OnBorderListener {

/**
* Called when scroll to bottom
*/
public void onBottom();

/**
* Called when scroll to top
*/
public void onTop();
}

private void doOnBorderListener() {
if (contentView != null && contentView.getMeasuredHeight() <= getScrollY() + getHeight()) {
if (onBorderListener != null) {
onBorderListener.onBottom();
}
} else if (getScrollY() == 0) {
if (onBorderListener != null) {
onBorderListener.onTop();
}
}
}
}

然后在另一个Fragmentactivity引用该子类:
public class FragmentActivity1 extends Fragment implements OnBorderListener

声明变量的时候出现cannot cast 的错误:

/* 以下代码报错:ScrollView cannot be cast to BorderScrollView */
BorderScrollView borderScrollView = (BorderScrollView)this.getActivity().findViewById(R.id.orderlist_scrollview);

我就奇了怪了,BorderScrollView创建不是继承ScrollView吗,怎么会出现这个问题????
...全文
116 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
阿不阿 2014-04-11
  • 打赏
  • 举报
回复
非常感谢,Logcat的时候看到了报错信息,找不到android.view.BorderScrollVIew
引用 5 楼 hjywyj 的回复:

<?xml version="1.0" encoding="utf-8"?>
<net.iwetao.init.BorderScrollView     xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" 
    android:layout_height="match_parent"
    android:scrollbars="vertical"
    android:paddingBottom="48dip"
    android:id="@+id/orderlist_scrollview">
 
<!-- 订单列表区域 -->
<LinearLayout android:id="@+id/orderlist_content"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dip"
    android:orientation="vertical">
</LinearLayout>
 
</net.iwetao.init.BorderScrollView>
  • 打赏
  • 举报
回复

<?xml version="1.0" encoding="utf-8"?>
<net.iwetao.init.BorderScrollView     xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" 
    android:layout_height="match_parent"
    android:scrollbars="vertical"
    android:paddingBottom="48dip"
    android:id="@+id/orderlist_scrollview">
 
<!-- 订单列表区域 -->
<LinearLayout android:id="@+id/orderlist_content"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dip"
    android:orientation="vertical">
</LinearLayout>
 
</net.iwetao.init.BorderScrollView>
阿不阿 2014-04-11
  • 打赏
  • 举报
回复
把xml中的ScrollView改成BorderScrollView吗?
引用 3 楼 hjywyj 的回复:
布局中你用的ScrollView,强制转换为BorderScrollView 当然报错了。
  • 打赏
  • 举报
回复
布局中你用的ScrollView,强制转换为BorderScrollView 当然报错了。
阿不阿 2014-04-11
  • 打赏
  • 举报
回复
这跟 xml也有关系?

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" 
    android:layout_height="match_parent"
    android:scrollbars="vertical"
    android:paddingBottom="48dip"
    android:id="@+id/orderlist_scrollview">

<!-- 订单列表区域 -->
<LinearLayout android:id="@+id/orderlist_content"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dip"
    android:orientation="vertical">
</LinearLayout>

</ScrollView>
引用 1 楼 hjywyj 的回复:
xml贴出来看看
  • 打赏
  • 举报
回复
xml贴出来看看

80,350

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧