Android - PullToRefresh ClassCastException

时间:2014-11-02 10:04:30

标签: java android classcastexception

请耐心等待我;因为,我是Android / Java的新手。

我使用PullToRefresh库(来自Chrisbanes)并为我的Android应用程序安装了LoadMoreGridView。当我运行应用程序时,遇到ClassCassException:

java.lang.ClassCastException: com.handmark.pulltorefresh.library.PullToRefreshGridView cannot be cast to com.mycommon.utils.LoadMoreGridView    

坦率地说,我无法弄清楚我是否将正确的类型转换为LoadMoreGridView。以下是我的部分GridFragment.java,它执行强制转换为LoadMoreGridView:

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshGridView;
import com.mycommom.utils.LoadMoreGridView;
import com.mycommom.utils.LoadMoreGridView.OnLoadMoreListener;

public class GridFragment extends Fragment {
...
private PullToRefreshGridView mPullRefreshGridView;
private LoadMoreGridView mGridView;
...
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
...
        mPullRefreshGridView = (PullToRefreshGridView) v.findViewById(R.id.feedgrid);
        mGridView = (LoadMoreGridView) mPullRefreshGridView.getRefreshableView();
...

以下是部分com.mycommon.utils.LoadMoreGridView:

public class LoadMoreGridView
  extends GridView
  implements AbsListView.OnScrollListener
{
  private static final String TAG = "LoadMoreListView";
  private AbsListView.OnScrollListener mOnScrollListener;
  private LayoutInflater mInflater;
  private RelativeLayout mFooterView;
  private ProgressBar mProgressBarLoadMore;
  private OnLoadMoreListener mOnLoadMoreListener;
  private boolean mIsLoadingMore = false;
  private int mCurrentScrollState;

  public LoadMoreGridView(Context context)
  {
     super(context);

     init(context);
  }

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

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

  private void init(Context context)
  {
     this.mInflater = 
       ((LayoutInflater)context.getSystemService("layout_inflater"));

     super.setOnScrollListener(this);
  }

...

这是Chrisbanes的PullRefreshGridView.java:

import ...
public class PullToRefreshGridView extends PullToRefreshAdapterViewBase<GridView> {

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

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

public PullToRefreshGridView(Context context, Mode mode) {
    super(context, mode);
}

public PullToRefreshGridView(Context context, Mode mode, AnimationStyle style) {
    super(context, mode, style);
}

@Override
public final Orientation getPullToRefreshScrollDirection() {
    return Orientation.VERTICAL;
}

@Override
protected final GridView createRefreshableView(Context context, AttributeSet attrs) {
    final GridView gv;
    if (VERSION.SDK_INT >= VERSION_CODES.GINGERBREAD) {
        gv = new InternalGridViewSDK9(context, attrs);
    } else {
        gv = new InternalGridView(context, attrs);
    }

    // Use Generated ID (from res/values/ids.xml)
    gv.setId(R.id.gridview);
    return gv;
}

...

    @TargetApi(9)
    final class InternalGridViewSDK9 extends InternalGridView {

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

    @Override
    protected boolean overScrollBy(int deltaX, int deltaY, int scrollX, int scrollY, int scrollRangeX,
            int scrollRangeY, int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent) {

        final boolean returnValue = super.overScrollBy(deltaX, deltaY, scrollX, scrollY, scrollRangeX,
                scrollRangeY, maxOverScrollX, maxOverScrollY, isTouchEvent);


     OverscrollHelper.overScrollBy(PullToRefreshGridView.this, deltaX, scrollX, deltaY, scrollY, isTouchEvent);

        return returnValue;
          }
    }
}

按要求我的主要xml:

<FrameLayout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLogin"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".MainActivity1" >

<com.zeal.peak.obejects.RecyclingImageView
    android:id="@+id/iv1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

<LinearLayout
    android:id="@+id/unspacedlayot"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin" >

      <ImageView
            android:id="@+id/ivmyappz_logo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:scaleType="center"
            android:layout_gravity="center"
            android:layout_marginBottom="10dp"
            android:background="@drawable/ic_myappz_title" />
    <LinearLayout
        android:id="@+id/unamelyt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/buttonbg1"
            android:scaleType="center"
            android:src="@drawable/login_email" />

        <EditText
            android:id="@+id/et_uname"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@drawable/coustom_editext"
            android:ems="10"
            android:hint="Email"
            android:inputType="textEmailAddress"
            android:textColor="@android:color/black"
            android:textColorHint="@color/font_color" >

            <requestFocus />
        </EditText>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/passlayt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="1dp" >

        <ImageView
            android:id="@+id/imageView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/buttonbg1"
            android:scaleType="center"
            android:src="@drawable/login_password" />

        <EditText
            android:id="@+id/et_pass"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@drawable/coustom_editext"
            android:ems="10"
            android:hint="Password"
            android:inputType="textPassword"
            android:textColor="@android:color/black"
            android:textColorHint="@color/font_color" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/laytbtns"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp" >

        <Button
            android:id="@+id/btn_login"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            android:layout_weight="1"
            android:background="@drawable/login" />

        <Button
            android:id="@+id/btn_register"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_weight="1"
            android:background="@drawable/register" />
    </LinearLayout>

    <Button
        android:id="@+id/btn_fblogin"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:background="@drawable/loginthroughfb" />

    <TextView
        android:id="@+id/tv_frgtpassword"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="5dp"
        android:gravity="center"
        android:text="@string/lbl_forget_password"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textSize="15sp" />
</LinearLayout>

</FrameLayout>

对上述问题的任何帮助都非常感谢。谢谢!

2 个答案:

答案 0 :(得分:0)

代码试图将对象强制转换为不是实例的子类。例如,以下代码生成ClassCastException:

 Object x = new Integer(0);
 System.out.println((String)x);

答案 1 :(得分:0)

您的xml中似乎LoadMoreGridView下有R.id.feedgrid。它应该是PullToRefreshGridView。此外,当您尝试将mPullRefreshGridView.getRefreshableView()转换为LoadMoreGridView时,我会认为会导致另一个异常,因为在InternalGridView中创建了createRefreshableView,而不是LoadMoreGridView