0

我正在开发一个以 ExpandableListView 布局显示数据的应用程序。单击 GroupItem 后应用程序停止。我的 GroupItem 有图像图标和指示器。如果我从 GroupItem 中删除图像,它会完美运行。所以我假设我的 group.xml 有问题,但我想不通。

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

日志猫:

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)

根据 Logcat,我在设置图像的第 142 行得到了 NullPointException。

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

Java 代码:

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

1 回答 1

1

移动

GroupViewHolder holder = new GroupViewHolder();

在您的if声明中。您每次都在创建一个新实例,这使得mIcon null每次。它应该像

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);
    }  
于 2013-06-04T20:02:15.777 回答