Michael
Michael

Reputation: 375

Application stops due to XML file

I'm working on an application that displays data in ExpandableListView layout. The application is stopped after I click on the GroupItem. My GroupItem has image icon and indicator. If I remove the image from the GroupItem, it works perfect. So I'm assuming there is something wrong with my group.xml, but I couldn't figure out.

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

    <TextView
        android:id="@+id/group_name"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_marginLeft="35dip"
        android:gravity="center_vertical"
        android:singleLine="true"
        android:textSize="14sp"
        android:textStyle="bold" />

    <ImageView
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="2dip"
        android:layout_marginRight="10dip" />

</LinearLayout>

LogCat:

06-04 19:52:51.562: I/Choreographer(4599): Skipped 172 frames!  The application may be doing too much work on its main thread.
06-04 19:52:58.020: D/AndroidRuntime(4599): Shutting down VM
06-04 19:52:58.020: W/dalvikvm(4599): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
06-04 19:52:58.140: E/AndroidRuntime(4599): FATAL EXCEPTION: main
06-04 19:52:58.140: E/AndroidRuntime(4599): java.lang.NullPointerException
06-04 19:52:58.140: E/AndroidRuntime(4599):     at com.example.wip.ExpandableListViewAdapter.getGroupView(ExpandableListViewAdapter.java:142)
06-04 19:52:58.140: E/AndroidRuntime(4599):     at android.widget.ExpandableListConnector.getView(ExpandableListConnector.java:446)

According to Logcat, I got NullPointException in line 142 where I set the image.

holder.mIcon.setImageResource(R.drawable.icon);

Java Code:

public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        GroupViewHolder holder = new GroupViewHolder();
        if (convertView == null) {  
            convertView = minflater.inflate(R.layout.grouprow, null);  
            holder.mIcon = (ImageView) convertView.findViewById(R.id.img);
        }  
        holder.mIcon.setImageResource(R.drawable.cvsm);


        holder.mGroupName = (TextView) convertView  
                .findViewById(R.id.group_name);  
        holder.mGroupName.setText(groupItem.get(groupPosition));  

        return convertView;  


    }

private class GroupViewHolder {
        ImageView mIcon ; 
        TextView mGroupName;  
    }  

Upvotes: 0

Views: 64

Answers (1)

codeMagic
codeMagic

Reputation: 44571

Move

GroupViewHolder holder = new GroupViewHolder();

inside your if statement. You are creating a new instance each time which makes mIcon null everytime. It should be like

public View getGroupView(int groupPosition, boolean isExpanded,
        View convertView, ViewGroup parent) {

    GroupViewholder holder;
    if (convertView == null) {  
        holder = new GroupViewHolder();  // try putting it here
        convertView = minflater.inflate(R.layout.grouprow, null);  
        holder.mIcon = (ImageView) convertView.findViewById(R.id.img);
    }  

Upvotes: 1

Related Questions