0

我正在尝试在其中一个选项卡片段中创建一个可扩展列表。我收到空指针错误,但我无法修复它...问题出现在本节:expandableListView.setAdapter(adapter); 任何帮助,将不胜感激。

堆栈跟踪(错误):

      10-20 12:39:31.603: E/AndroidRuntime(3478): FATAL EXCEPTION: main
10-20 12:39:31.603: E/AndroidRuntime(3478): java.lang.NullPointerException
10-20 12:39:31.603: E/AndroidRuntime(3478):     at com.margaret.app.easypeasycooking.CountryAdapter.getGroupCount(CountryAdapter.java:119)
10-20 12:39:31.603: E/AndroidRuntime(3478):     at android.widget.ExpandableListConnector.getCount(ExpandableListConnector.java:397)
10-20 12:39:31.603: E/AndroidRuntime(3478):     at android.widget.ListView.setAdapter(ListView.java:463)
10-20 12:39:31.603: E/AndroidRuntime(3478):     at android.widget.ExpandableListView.setAdapter(ExpandableListView.java:599)
10-20 12:39:31.603: E/AndroidRuntime(3478):     at com.margaret.app.easypeasycooking.ShoppingList.onCreateView(ShoppingList.java:37)
10-20 12:39:31.603: E/AndroidRuntime(3478):     at android.app.Fragment.performCreateView(Fragment.java:1695)
10-20 12:39:31.603: E/AndroidRuntime(3478):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:885)
10-20 12:39:31.603: E/AndroidRuntime(3478):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
10-20 12:39:31.603: E/AndroidRuntime(3478):     at android.app.BackStackRecord.run(BackStackRecord.java:682)
10-20 12:39:31.603: E/AndroidRuntime(3478):     at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
10-20 12:39:31.603: E/AndroidRuntime(3478):     at android.app.FragmentManagerImpl$1.run(FragmentManager.java:441)
10-20 12:39:31.603: E/AndroidRuntime(3478):     at android.os.Handler.handleCallback(Handler.java:730)
10-20 12:39:31.603: E/AndroidRuntime(3478):     at android.os.Handler.dispatchMessage(Handler.java:92)
10-20 12:39:31.603: E/AndroidRuntime(3478):     at android.os.Looper.loop(Looper.java:137)
10-20 12:39:31.603: E/AndroidRuntime(3478):     at android.app.ActivityThread.main(ActivityThread.java:5103)
10-20 12:39:31.603: E/AndroidRuntime(3478):     at java.lang.reflect.Method.invokeNative(Native Method)
10-20 12:39:31.603: E/AndroidRuntime(3478):     at java.lang.reflect.Method.invoke(Method.java:525)
10-20 12:39:31.603: E/AndroidRuntime(3478):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-20 12:39:31.603: E/AndroidRuntime(3478):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-20 12:39:31.603: E/AndroidRuntime(3478):     at dalvik.system.NativeStart.main(Native Method)

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>
4

1 回答 1

1
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)

ShoppingList第 37 行,您尝试将 a 强制RelativeLayout转换为ExandableListView.

在这一行中,expandableListView = (ExpandableListView) rootView.findViewById(R.id.shopping); R.id.shopping指的是相对布局,而不是您的列表视图。

编辑您的问题以包含 XML,我们可以确认这一点。

于 2013-10-20T15:16:48.940 回答