应用程序因XML文件而停止

时间:2013-06-04 19:49:39

标签: java android

我正在开发一个在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>

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)

根据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;  
    }  

1 个答案:

答案 0 :(得分: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);
    }