Android ExpandableListView NullPointerException at ObtainView
Asked Answered
L

1

1

I'm trying to work with a simple ExpandableListView and I can't figure out why I get that NPE at ObtainView. I should have only one parent and few more children which should be opened on click on the parent of them, but it keeps crashing and showing NullPointerException on ObtainView. Thanks a lot !

Prima.java:

package com.descoper.rom;

import java.util.ArrayList;

import android.app.ExpandableListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.ExpandableListView;

public class Prima extends ExpandableListActivity
{
    // Create ArrayList to hold parent Items and Child Items
    private ArrayList<String> parentItems = new ArrayList<String>();
    private ArrayList<Object> childItems = new ArrayList<Object>();

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {

        super.onCreate(savedInstanceState);


        // Create Expandable List and set it's properties
        ExpandableListView expandableList = getExpandableListView(); 
        expandableList.setDividerHeight(2);
        expandableList.setGroupIndicator(null);
        expandableList.setClickable(true);

        // Set the Items of Parent
        setGroupParents();
        // Set The Child Data
        setChildData();

        // Create the Adapter
        Adapter adapter = new Adapter(parentItems, childItems);

        adapter.setInflater((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE), this);

        // Set the Adapter to expandableList
        expandableList.setAdapter(adapter);
        expandableList.setOnChildClickListener(this);
    }

    // method to add parent Items
    public void setGroupParents() 
    {
        parentItems.add("Categorii");
    }

    // method to set child data of each parent
    public void setChildData() 
    {

        // Add Child Items for Fruits
        ArrayList<String> child = new ArrayList<String>();
        child.add("Apple");
        child.add("Mango");
        child.add("Banana");
        child.add("Orange");

        childItems.add(child);

    }

}

Adapter.java:

package com.descoper.rom;

import java.util.ArrayList;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckedTextView;
import android.widget.TextView;
import android.widget.Toast;

public class Adapter extends BaseExpandableListAdapter 
{

    private Activity activity;
    private ArrayList<Object> childtems;
    private LayoutInflater inflater;
    private ArrayList<String> parentItems, child;

    // constructor
    public Adapter(ArrayList<String> parents, ArrayList<Object> childern)
    {
        this.parentItems = parents;
        this.childtems = childern;
    }

    public void setInflater(LayoutInflater inflater, Activity activity) 
    {
        this.inflater = inflater;
        this.activity = activity;
    }

    // method getChildView is called automatically for each child view.
    public View getChildView1(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) 
    {

        child = (ArrayList<String>) childtems.get(groupPosition);

        TextView textView = null;

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.child, null);
        }

         // get the textView reference and set the value
        textView = (TextView) convertView.findViewById(R.id.textViewChild);
        textView.setText(child.get(childPosition));

        // set the ClickListener to handle the click event on child item
        convertView.setOnClickListener(new OnClickListener() {

            public void onClick(View view) {
                Toast.makeText(activity, child.get(childPosition),
                        Toast.LENGTH_SHORT).show();
            }
        });
        return convertView;
    }

    // method getGroupView is called automatically for each parent item
    public View getGroupView1(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) 
    {

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.categorii, null);
        }

        ((CheckedTextView) convertView).setText(parentItems.get(groupPosition));
        ((CheckedTextView) convertView).setChecked(isExpanded);

        return convertView;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) 
    {
        return null;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) 
    {
        return 0;
    }

    @Override
    public int getChildrenCount(int groupPosition) 
    {
        return ((ArrayList<String>) childtems.get(groupPosition)).size();
    }

    @Override
    public Object getGroup(int groupPosition) 
    {
        return null;
    }

    @Override
    public int getGroupCount() 
    {
        return parentItems.size();
    }

    @Override
    public void onGroupCollapsed(int groupPosition) 
    {
        super.onGroupCollapsed(groupPosition);
    }

    @Override
    public void onGroupExpanded(int groupPosition)
    {
        super.onGroupExpanded(groupPosition);
    }

    @Override
    public long getGroupId(int groupPosition) 
    {
        return 0;
    }

    @Override
    public boolean hasStableIds() 
    {
        return false;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition)
    {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        return null;
    }

}

parent.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/backg1"
    android:gravity="center|top"
     >

    <CheckedTextView 
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/textViewGroupName"
        android:layout_width="wrap_content"
        android:layout_height="60dp"
        android:layout_marginLeft="5dp"
        android:gravity="center_vertical"
        android:text="Categorii"
        android:textSize="18dp"
        android:textColor="#FFFFFF"
        android:padding="10dp"
        android:textStyle="bold" /> 

</LinearLayout>

child.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="40dp"

    android:clickable="true"
    android:orientation="vertical"
    android:paddingLeft="40dp"
     >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="39dp"
        android:gravity="center_vertical" >

        <ImageView
            android:id="@+id/childImage"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_margin="5dp"
            android:background="@drawable/ic_launcher"
            android:contentDescription="@null" />

        <TextView
            android:id="@+id/textViewChild"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:text="Categorii"
            android:textSize="16sp"
            android:textColor="#1919A3"
            android:textStyle="bold" />
    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@android:color/white" />

</LinearLayout>

logcat:

05-19 10:18:25.180: E/AndroidRuntime(1361): FATAL EXCEPTION: main
05-19 10:18:25.180: E/AndroidRuntime(1361): Process: com.descoper.rom, PID: 1361
05-19 10:18:25.180: E/AndroidRuntime(1361): java.lang.NullPointerException
05-19 10:18:25.180: E/AndroidRuntime(1361):     at android.widget.AbsListView.obtainView(AbsListView.java:2265)
05-19 10:18:25.180: E/AndroidRuntime(1361):     at android.widget.ListView.makeAndAddView(ListView.java:1790)
05-19 10:18:25.180: E/AndroidRuntime(1361):     at android.widget.ListView.fillDown(ListView.java:691)
05-19 10:18:25.180: E/AndroidRuntime(1361):     at android.widget.ListView.fillFromTop(ListView.java:752)
05-19 10:18:25.180: E/AndroidRuntime(1361):     at android.widget.ListView.layoutChildren(ListView.java:1630)
05-19 10:18:25.180: E/AndroidRuntime(1361):     at android.widget.AbsListView.onLayout(AbsListView.java:2091)
05-19 10:18:25.180: E/AndroidRuntime(1361):     at android.view.View.layout(View.java:14817)
05-19 10:18:25.180: E/AndroidRuntime(1361):     at android.view.ViewGroup.layout(ViewGroup.java:4631)
05-19 10:18:25.180: E/AndroidRuntime(1361):     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
05-19 10:18:25.180: E/AndroidRuntime(1361):     at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
05-19 10:18:25.180: E/AndroidRuntime(1361):     at android.view.View.layout(View.java:14817)
05-19 10:18:25.180: E/AndroidRuntime(1361):     at android.view.ViewGroup.layout(ViewGroup.java:4631)
05-19 10:18:25.180: E/AndroidRuntime(1361):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
05-19 10:18:25.180: E/AndroidRuntime(1361):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
05-19 10:18:25.180: E/AndroidRuntime(1361):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
05-19 10:18:25.180: E/AndroidRuntime(1361):     at android.view.View.layout(View.java:14817)
05-19 10:18:25.180: E/AndroidRuntime(1361):     at android.view.ViewGroup.layout(ViewGroup.java:4631)
05-19 10:18:25.180: E/AndroidRuntime(1361):     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
05-19 10:18:25.180: E/AndroidRuntime(1361):     at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
05-19 10:18:25.180: E/AndroidRuntime(1361):     at android.view.View.layout(View.java:14817)
05-19 10:18:25.180: E/AndroidRuntime(1361):     at android.view.ViewGroup.layout(ViewGroup.java:4631)
05-19 10:18:25.180: E/AndroidRuntime(1361):     at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1987)
05-19 10:18:25.180: E/AndroidRuntime(1361):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1744)
05-19 10:18:25.180: E/AndroidRuntime(1361):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
05-19 10:18:25.180: E/AndroidRuntime(1361):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
05-19 10:18:25.180: E/AndroidRuntime(1361):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
05-19 10:18:25.180: E/AndroidRuntime(1361):     at android.view.Choreographer.doCallbacks(Choreographer.java:574)
05-19 10:18:25.180: E/AndroidRuntime(1361):     at android.view.Choreographer.doFrame(Choreographer.java:544)
05-19 10:18:25.180: E/AndroidRuntime(1361):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
05-19 10:18:25.180: E/AndroidRuntime(1361):     at android.os.Handler.handleCallback(Handler.java:733)
05-19 10:18:25.180: E/AndroidRuntime(1361):     at android.os.Handler.dispatchMessage(Handler.java:95)
05-19 10:18:25.180: E/AndroidRuntime(1361):     at android.os.Looper.loop(Looper.java:136)
05-19 10:18:25.180: E/AndroidRuntime(1361):     at android.app.ActivityThread.main(ActivityThread.java:5017)
05-19 10:18:25.180: E/AndroidRuntime(1361):     at java.lang.reflect.Method.invokeNative(Native Method)
05-19 10:18:25.180: E/AndroidRuntime(1361):     at java.lang.reflect.Method.invoke(Method.java:515)
05-19 10:18:25.180: E/AndroidRuntime(1361):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-19 10:18:25.180: E/AndroidRuntime(1361):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-19 10:18:25.180: E/AndroidRuntime(1361):     at dalvik.system.NativeStart.main(Native Method)
Leahy answered 19/5, 2014 at 14:22 Comment(0)
M
0

You are returning null from getChild() and getGroup(). You should return the corresponding group and child items from your data structures.

Also, you are returning null from getChildView() and getGroupView(). Here you should return a View that will represent the child and group Views respectively.

I suggest you read about Adapters and how they work.

Martyr answered 19/5, 2014 at 14:30 Comment(4)
Can you tell me exactly how can I return the corresponding group and child items ? Thanks a lot !Leahy
Check the tutorial I linked above.Martyr
I read that. But can you show me speific on my case how to return to be correct? Thanks !Leahy
@Leahy - I am also getting similar issue. can you please tell me how did you fix this issue?Glowworm

© 2022 - 2024 — McMap. All rights reserved.