2012-04-16 8 views
1

これは変です。何らかの理由でGridViewのサブクラスを適切にキャストするのが苦労しています。 CustomGridViewをキャストしようとするとCaused by: java.lang.ClassCastException: android.widget.GridView cannot be cast to com.dobydigital.dobixchange.CustomGridViewが得られます。ここでGridViewをサブクラス化するときにClassCastExceptionが発生するのはなぜですか?

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

FileBrowserFragment.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); 

file_browser.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: [email protected]/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> 

私は何が起こっているのかを把握することはできません

CustomGridView.java私のコードです。以前はレイアウトのサブクラス化に問題はなかった。

+0

'getViewメソッドは、()'何をしますか?また、正しい 'R'ファイルをインポートしていますか? –

+0

カスタムGridViewを通常のGridViewにキャストする必要はありませんが、実際には必要ありません。カスタムGridViewで何をしようとしていますか? –

+0

私はFragment内でgetView()を使用してインフレしたビューへの参照を取得する必要があります(この場合、ルートノード、LinearLayoutです)。 Fragmentクラスには単純な「findViewById()」はありません。 –

答えて

0

をgetViewメソッドを取り除く:あなたは誤っonCreateView()に間違ったレイアウトを返す&膨らま:)

+0

助けてくれてありがとう=) –

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

質問のコメントより)(

+0

私はできません、これは断片の中です。 –

関連する問題