Android 使用片段中的可扩展列表时出现空指针错误

Android 使用片段中的可扩展列表时出现空指针错误,android,Android,我正在尝试在一个选项卡片段中创建一个可扩展的列表。 我得到了空指针错误,但我无法修复它。。。 此部分出现问题: expandableListView.setAdapter(适配器); 任何帮助都将不胜感激 StackTrace(错误): XML: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

我正在尝试在一个选项卡片段中创建一个可扩展的列表。 我得到了空指针错误,但我无法修复它。。。 此部分出现问题: expandableListView.setAdapter(适配器); 任何帮助都将不胜感激

StackTrace(错误):

XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:id="@+id/shopping">

    <ExpandableListView
        android:id="@+id/elvShoppingList"
        android:layout_width="wrap_content"
        android:layout_height="400dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >
    </ExpandableListView>

    <RadioButton
        android:id="@+id/radioButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="18dp"
        android:text="RadioButton" />

</RelativeLayout>

ShoppingList
第37行,您试图将
RelativeLayout
转换为
ExandableListView

在这一行中,
expandableListView=(expandableListView)rootView.findviewbyd(R.id.shopping)R.id.shopping指的是一个RelativeLayout,而不是您的列表视图


编辑您的问题以包含XML,我们可以对此进行确认。

请从logcat发布堆栈跟踪。它准确地显示了错误所在的位置。您还应该学习使用调试器。NullPointerException很容易找到和修复,调试器可以让你在几分钟内完成。你能发布堆栈跟踪吗?@AdamToth刚刚添加了堆栈跟踪。@Simon-谢谢你的提示。我将尝试使用它。我没想过。这不是空指针例外!expandableListView=(expandableListView)rootView.findViewById(R.id.shopping);这显然是一个相对的应用程序,您正在将其强制转换为ExpandableListView我已更改ExpandableListView=(ExpandableListView)根视图。findViewById(R.id.shopping);-->并运行它;我已经附加了XML和更新的堆栈。现在您可以看到这是空指针异常错误。。。。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:id="@+id/shopping">

    <ExpandableListView
        android:id="@+id/elvShoppingList"
        android:layout_width="wrap_content"
        android:layout_height="400dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >
    </ExpandableListView>

    <RadioButton
        android:id="@+id/radioButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="18dp"
        android:text="RadioButton" />

</RelativeLayout>
java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.ExpandableListView
10-20 10:53:08.892: E/AndroidRuntime(2636):     at com.margaret.app.easypeasycooking.ShoppingList.onCreateView(ShoppingList.java:37)