2014-11-02 2 views
0

Прошу вас поучаствовать в этом; так как я новичок в Android/Java.Android - PullToRefresh ClassCastException

Я использую библиотеку PullToRefresh (от Chrisbanes) и имею LoadMoreGridView для моего приложения для Android. Когда я запустить приложение, я сталкиваюсь 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); 
    } 

... 

И это PullRefreshGridView. ява по 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

пожалуйста, покажите ваш XML данного зрения –

+0

Спасибо. Я отправил основной xml по запросу. – Bean

ответ

0

Код попытался передать объект в подкласс, экземпляром которого он не является. Например, следующий код создает ClassCastException:

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

Благодарим вас за ответ. Я все еще запутался, посмотрев на PullRefreshGridView.java и LoadMoreGridView.java, он должен иметь возможность делать трансляции в моем GridFragment.java: mGridView = (LoadMoreGridView) mPullRefreshGridView.getRefreshableView(); или, может быть, я здесь неправ? Спасибо – Bean

0

Кажется, что есть LoadMoreGridView под R.id.feedgrid в вашем XML. Вместо этого оно должно быть PullToRefreshGridView. Кроме того, когда вы пытаетесь бросить mPullRefreshGridView.getRefreshableView() к LoadMoreGridView будет причиной еще одно исключение, я думаю, потому что есть InternalGridView создание в createRefreshableView, не LoadMoreGridView

+0

Спасибо, Ромаджа за ответ. Не могли бы вы более подробно рассказать о том, как создать InternalGridView в createRefreshableView, а не LoadMoreGridView. Спасибо за вашу помощь. – Bean

+0

Посмотрите на метод 'createRefreshableView' вашего' PullToRefreshGridView'. Вы можете видеть, что существует 'gv = new InternalGridView (context, attrs)'. Это означает, что при попытке 'getRefreshableView()' вы получите 'InternalGridView'. Но вы пытаетесь передать его в «LoadMoreGridView», это неправильно – rom4ek

Смежные вопросы