LoginSignup
1
0

More than 5 years have passed since last update.

フラグメントでExpandableListViewを使用する場合、継承をListFragmentにすると例外発生

Posted at

フラグメントでExpandableListViewを使用する

fragment1.xml
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:paddingBottom="0dp"
    android:paddingLeft="30dp"
    android:paddingRight="0dp"
    android:paddingTop="0dp" >

    <ExpandableListView
        android:id="@+id/expListView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:paddingLeft="5dp"
        android:paddingTop="5dp"
        android:background="#003060"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:dividerHeight="3dp" />
</RelativeLayout>

ExpandableListViewを使用する場合、フラグメントを実装するクラスの継承をListFragmentにしてしまうと、

fragment1.java
public class Fragment1  extends ListFragment {

onViewCreatedRuntimeExceptionが発生します。
(正確にはスーパークラス、つまりListFragmentのonViewCreatedで例外が投げられる)

 "Content has view with id attribute 'android.R.id.list' that is not a ListView class"

例外の回避

これを回避するには、継承をListFragmentではなくFragmentにします。

fragment1.java
public class Fragment1  extends Fragment {
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0