2014-11-02 68 views
0

請與我在這一個;因爲我是Android/Java的新手。Android - PullToRefresh ClassCastException

我使用PullToRefresh庫(來自Chrisbanes)併爲我的Android應用程序使用LoadMoreGridView。當我運行應用程序,我遇到ClassCassException:

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

坦率地說,我無法弄清楚我是否投正確類型LoadMoreGridView。以下是我認爲做演員到LoadMoreGridView部分GridFragment.java:

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

... 

這是PullRefreshGridView。 java編寫Chrisbanes:

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> 

對上述問題的任何幫助真的很感激。謝謝!

+0

請出示您的給定視圖 – 2014-11-02 10:24:19

+0

謝謝你的XML。我根據請求發佈了主要的xml。 – Bean 2014-11-02 10:49:40

回答

0

該代碼試圖將對象轉換爲其不是實例的子類。例如,下面的代碼生成一個ClassCastException:

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

謝謝你的回答。我仍然很困惑,通過查看PullRefreshGridView.java和LoadMoreGridView.java,它應該能夠在我的GridFragment.java中進行轉換:mGridView =(LoadMoreGridView)mPullRefreshGridView.getRefreshableView();或者,也許我在這裏錯了?謝謝 – Bean 2014-11-02 10:14:55

0

似乎有在XML R.id.feedgridLoadMoreGridView。應改爲PullToRefreshGridView。此外,當您嘗試投mPullRefreshGridView.getRefreshableView()LoadMoreGridView會導致另一個例外,我認爲,這是因爲InternalGridView創造createRefreshableView,不LoadMoreGridView

+0

謝謝Romadja的回答。你能不能更詳細地闡述「在createRefreshableView中創建的InternalGridView,而不是LoadMoreGridView」謝謝你的幫助。 – Bean 2014-11-03 07:58:03

+0

看看你的'PullToRefreshGridView'的'createRefreshableView'方法。你可以看到'gv = new InternalGridView(context,attrs)'。這意味着當你嘗試'getRefreshableView()'時,你會得到'InternalGridView'。但是你試圖將它轉換成'LoadMoreGridView',這是錯誤的 – rom4ek 2014-11-03 10:46:52