1

真奇怪; 出于某种原因,我很难让 GridView 的子类正确转换。我Caused by: java.lang.ClassCastException: android.widget.GridView cannot be cast to com.dobydigital.dobixchange.CustomGridView在尝试投射我的 CustomGridView 时得到。这是我的代码

CustomGridView.java

import android.widget.GridView;
public class CustomGridView extends GridView {

文件浏览器片段.java

//# Other stuff omitted for brevity
private CustomGridView gridView;
public class FileBrowserFragment extends Fragment {
//# Stuff
}
//# .........
@Override
public void onActivityCreated(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onActivityCreated(savedInstanceState);
    //# This cast here throws the exception
    gridView = (CustomGridView) getView().findViewById(R.id.grid_view);

文件浏览器.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/root_node"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

<com.dobydigital.dobixchange.CustomGridView
    android:id="@+id/grid_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:numColumns="3"
    android:layout_weight="80" >
    <!-- Preview: listitem=@layout/image_tile -->
</com.dobydigital.dobixchange.CustomGridView>



<Button
    android:id="@+id/btn_confirm"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:text="Select this Directory" />

</LinearLayout>

我不知道发生了什么事。我以前从未遇到过子类化布局的问题。

4

2 回答 2

0
gridView = (CustomGridView) findViewById(R.id.grid_view);

摆脱 getView()

于 2012-04-16T20:35:46.730 回答
0

从问题的评论:你不小心膨胀并返回错误的布局onCreateView():)

于 2012-04-16T22:50:49.740 回答