Android:滚动ExpandableListView时发生NullPointerException

时间:2013-07-19 08:00:55

标签: android nullpointerexception expandablelistview

07-17 15:57:45.523: W/dalvikvm(6777): threadid=1: thread exiting with uncaught exception (group=0x4001d5a0)
07-17 15:57:45.583: W/System.err(6777): java.lang.NullPointerException
07-17 15:57:45.583: W/System.err(6777):     at android.widget.TextView.onTouchEvent(TextView.java:7503)
07-17 15:57:45.583: W/System.err(6777):     at android.view.View.dispatchTouchEvent(View.java:3933)
07-17 15:57:45.583: W/System.err(6777):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:906)
07-17 15:57:45.583: W/System.err(6777):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:906)
07-17 15:57:45.583: W/System.err(6777):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:906)
07-17 15:57:45.583: W/System.err(6777):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:906)
07-17 15:57:45.593: W/System.err(6777):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:906)
07-17 15:57:45.593: W/System.err(6777):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1881)
07-17 15:57:45.593: W/System.err(6777):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1216)
07-17 15:57:45.593: W/System.err(6777):     at android.app.Activity.dispatchTouchEvent(Activity.java:2228)
07-17 15:57:45.593: W/System.err(6777):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1856)
07-17 15:57:45.593: W/System.err(6777):     at android.view.ViewRoot.deliverPointerEvent(ViewRoot.java:2382)
07-17 15:57:45.593: W/System.err(6777):     at android.view.ViewRoot.handleMessage(ViewRoot.java:2010)
07-17 15:57:45.593: W/System.err(6777):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-17 15:57:45.593: W/System.err(6777):     at android.os.Looper.loop(Looper.java:150)
07-17 15:57:45.593: W/System.err(6777):     at android.app.ActivityThread.main(ActivityThread.java:4312)
07-17 15:57:45.593: W/System.err(6777):     at java.lang.reflect.Method.invokeNative(Native Method)
07-17 15:57:45.593: W/System.err(6777):     at java.lang.reflect.Method.invoke(Method.java:507)
07-17 15:57:45.593: W/System.err(6777):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
07-17 15:57:45.593: W/System.err(6777):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
07-17 15:57:45.603: W/System.err(6777):     at dalvik.system.NativeStart.main(Native Method)

当我在设备(Android 2.3)上快速向上和向下滚动时发生NullPointerException,但它在4.0版本的模拟器和设备上工作正常,我不知道发生了什么,希望你 伙计们可以提供帮助。

1 个答案:

答案 0 :(得分:0)

这是我的主要代码:

私有类DetailExpandAdapter扩展了BaseExpandableListAdapter {

    private Drawable isNull; 

    public DetailExpandAdapter(){
        isNull = getResources().getDrawable(R.drawable.star);
        //调用setCompoundDrawables时,必须调用Drawable.setBounds()方法,否则图片不显示
        isNull.setBounds(0, 0, isNull.getMinimumWidth(), isNull.getMinimumHeight());
    }

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

    @Override
    public int getChildrenCount(int groupPosition) {
        return child.get(groupPosition).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return group.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return child.get(groupPosition).get(childPosition);
    }

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

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return groupPosition * 100 + childPosition;
    }

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

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if(convertView == null){
            TextView text = new TextView(Update.this);
            text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
            text.setTextSize(15);
            text.setTextColor(Color.BLACK);
            AbsListView.LayoutParams lp;
            lp = new AbsListView.LayoutParams(
                     ViewGroup.LayoutParams.FILL_PARENT, ObjectUtils.getFontHeight(text.getTextSize()) * 2);
            text.setLayoutParams(lp);
            text.setBackgroundResource(R.drawable.expanditemgroup);
            convertView = text;
            holder = new ViewHolder();
            holder.key = (TextView)convertView;
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder)convertView.getTag();
        }
        ((TextView)convertView).setText(group.get(groupPosition));
        holder.key.setText(group.get(groupPosition));
        return convertView;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        final ConfigItem item=child.get(groupPosition).get(childPosition);
        if(convertView == null ){
            holder = new ViewHolder();
            convertView = LayoutInflater.from(Update.this).inflate(R.layout.queryitem, null);
            holder.isused = (CheckBox)convertView.findViewById(R.id.isused);
            holder.key = (TextView)convertView.findViewById(R.id.key);
            holder.edit_value = (EditText)convertView.findViewById(R.id.editvalue);
            holder.edit_value.setVisibility(View.VISIBLE);
            holder.text_value = (EditText)convertView.findViewById(R.id.textvalue);
            holder.btn = (ImageView)convertView.findViewById(R.id.set);
            holder.isused.setVisibility(View.GONE);

            LayoutParams params = holder.key.getLayoutParams();
            params.width = Update.this.getWindowManager().getDefaultDisplay().getWidth()/4;
            holder.key.setLayoutParams(params);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder)convertView.getTag();
        }

        holder.edit_value.clearFocus();
        holder.key.setText(item.getColumnDescribe() + ":");
        //增加必输项图标
        if (!item.isNull()) {
            holder.key.setCompoundDrawables(isNull, null, null, null);
        }else{
            holder.key.setCompoundDrawables(null, null, null, null);
        }

        //编辑框监听及赋值
        holder.edit_value.setOnFocusChangeListener(new OnFocusChangeListener() {

            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(!hasFocus){
                    item.setDisplayValue(((EditText)v).getText().toString());
                    item.setValue(((EditText)v).getText().toString());
                }
            }
        });
        if(item.isList()){
            holder.edit_value.setText(item.getDisplayValue());
            holder.text_value.setText(item.getDisplayValue());
        }else if(item.getColumnType().equals("VIRTUALBUTTON")){
            holder.edit_value.setText(item.getColumnDescribe());
            holder.text_value.setText(item.getColumnDescribe());
        }else{
            holder.edit_value.setText(item.getValue());
            holder.text_value.setText(item.getValue());
        }
        if(item.getColumnType().equalsIgnoreCase("number")){
            //only number is allowed when the columntype is number
            holder.text_value.setInputType(InputType.TYPE_CLASS_NUMBER);
            holder.edit_value.setInputType(InputType.TYPE_CLASS_NUMBER);
            holder.text_value.setSingleLine(true);
            holder.text_value.setLines(1);
            holder.edit_value.setSingleLine(true);
            holder.edit_value.setLines(1);
        } else if(item.getColumnType().equalsIgnoreCase("TEXTAREA")){
            holder.text_value.setInputType(InputType.TYPE_CLASS_TEXT);
            holder.edit_value.setInputType(InputType.TYPE_CLASS_TEXT);
            holder.text_value.setSingleLine(false);
            holder.text_value.setMinLines(3);
            holder.edit_value.setSingleLine(false);
            holder.edit_value.setMinLines(3);
        } else{
            holder.text_value.setInputType(InputType.TYPE_CLASS_TEXT);
            holder.edit_value.setInputType(InputType.TYPE_CLASS_TEXT);
            holder.text_value.setSingleLine(true);
            holder.text_value.setLines(1);
            holder.edit_value.setSingleLine(true);
            holder.edit_value.setLines(1);
        }


        if(item.isText()){
            holder.btn.setVisibility(View.GONE);
            //是否可编辑
            if(!item.isUpdate()){
                holder.text_value.setBackgroundDrawable(getResources().getDrawable(R.drawable.touming));
                holder.text_value.setEnabled(false);
                holder.text_value.setFocusable(false);
                holder.text_value.setFocusableInTouchMode(false);
                holder.text_value.setVisibility(View.VISIBLE);
                holder.edit_value.setVisibility(View.GONE);
            }else{
                holder.edit_value.setEnabled(true);
                holder.edit_value.setFocusable(true);
                holder.edit_value.setFocusableInTouchMode(true);
                holder.edit_value.setVisibility(View.VISIBLE);
                holder.text_value.setVisibility(View.GONE);
            }

        }else if(item.isList()){        //下拉框
            holder.btn.setVisibility(View.VISIBLE);
            if(!item.isUpdate()){
                holder.btn.setVisibility(View.GONE);

            }else{
                holder.btn.setVisibility(View.VISIBLE);

            }
            holder.edit_value.setVisibility(View.GONE);
            holder.text_value.setVisibility(View.VISIBLE);

            holder.btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    showGroupPosition = groupPosition;
                    showChildPosition = childPosition;
                    Intent intent = new Intent(Update.this, QueryDict.class);
                    intent.putExtra("class", ConstVars.DATASOURCE_LIST_DICT);
                    intent.putExtra("action", ConstVars.LN_DICT_ACTION);
                    intent.putExtra(ConstVars.DICT_ITEM, item);
                    intent.putExtra("fromClass", Update.this.getComponentName().getClassName());
                    startActivityForResult(intent, 1);
                }
            });
        }else if (child.get(groupPosition).get(childPosition).isDate()) {   
            holder.btn.setVisibility(View.VISIBLE);
            if(!item.isUpdate()){
                holder.btn.setVisibility(View.GONE);
            }else{
                holder.btn.setVisibility(View.VISIBLE);
            }

            holder.edit_value.setVisibility(View.GONE);
            holder.text_value.setVisibility(View.VISIBLE);

            holder.btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    showGroupPosition = groupPosition;
                    showChildPosition = childPosition;
                    Intent intent=new Intent(Update.this,DTPickerDialog.class);
                    intent.putExtra("fromClass", Update.this.getComponentName().getClassName());
                    Update.this.startActivityForResult(intent, 2);
                }
            });
        }else if(child.get(groupPosition).get(childPosition).isVitual()){
            holder.btn.setVisibility(View.VISIBLE);
            if(!item.isUpdate()){
                holder.btn.setVisibility(View.GONE);
            }else{
                holder.btn.setVisibility(View.VISIBLE);
            }
            if(child.get(groupPosition).get(childPosition).getColumnType().equalsIgnoreCase("VIRTUALBUTTON")
                            && child.get(groupPosition).get(childPosition).getLinkObject().getFuncName().equals("showFileField")){
                if(item.getValue()!=null&&!"".equals(item.getValue())){
                    holder.btn.setVisibility(View.VISIBLE);
                }else{
                    holder.btn.setVisibility(View.GONE);
                }
            }

            holder.edit_value.setVisibility(View.GONE);
            holder.text_value.setVisibility(View.VISIBLE);
            holder.text_value.setText(item.getName());

            holder.btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(child.get(groupPosition).get(childPosition).getColumnType().equalsIgnoreCase("USERPICKER")){
                        Intent intent=new Intent(Update.this, UserSelected.class);
                        showChildPosition=childPosition;
                        showGroupPosition=groupPosition;
                        intent.putExtra(ConstVars.USERCHOICE_SELECTUSER, item);
                        intent.putExtra("type", "13");

//
                            startActivityForResult(intent,10);                         } else if(child.get(groupPosition).get(childPosition).getColumnType()。equalsIgnoreCase(“VIRTUALBUTTON”)                                 &安培;&安培; child.get(groupPosition)获得(childPosition).getLinkObject()。getFuncName()。等于( “showFileField”)){//附件                             Intent intent = new Intent(Update.this,Attachment.class);                             intent.putExtra(“id”,item.getValue());

                        startActivity(intent);
                    }
                }
            });
        }

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

}