使用片段中的可扩展列表显示空指针错误

时间:2013-10-20 15:07:30

标签: android

我正在尝试在其中一个标签片段中创建可扩展列表。 我收到空指针错误,但我无法解决它... 本节中出现此问题: expandableListView.setAdapter(适配器); 任何帮助将不胜感激。

StackTrace(错误):

      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>

1 个答案:

答案 0 :(得分: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行,您正试图将RelativeLayout投放到ExandableListView

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

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