即使按钮被初始化,也会获得空指针

时间:2014-08-21 13:41:43

标签: android android-listview

我已经使用BaseAdapter显示自定义列表。现在我有一个按钮,但我得到空指针异常。我不知道为什么会发生这种情况,即使我已经初始化了按钮。

代码

public class ContactListAdapter extends BaseAdapter {

    private Context context;
    private ArrayList<ContactModel> data;
    DbHandler dbHandler;

    public ContactListAdapter(Context context, ArrayList<ContactModel> data) {
        this.context = context;
        this.data = data;
    }

    @Override

    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {

        final ViewHolder holder;
        dbHandler = new DbHandler(context);

        if (view == null) {
            holder = new ViewHolder();
            view = LayoutInflater.from(context).inflate(R.layout.contact_custom_list, viewGroup, false);
            holder.tvContact = (TextView) view.findViewById(R.id.tv_contact_name);
            holder.checkBox = (CheckBox) view.findViewById(R.id.cb_contact_checkbox);
            holder.btSend = (Button) view.findViewById(R.id.bt_sharecontact_send);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }

        holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (compoundButton == holder.checkBox) {
                    if (b) {
                        dbHandler.updateContactList(data.get(i).getUserID(), 1);
                    } else {
                        dbHandler.updateContactList(data.get(i).getUserID(), 0);
                    }
                }
            }
        });


        holder.tvContact.setText(data.get(i).getContactName());

        holder.btSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                StringBuilder values = new StringBuilder();
                values = dbHandler.getCheckedContact();
            }
        });


        return view;
    }

    private class ViewHolder {
        TextView tvContact;
        CheckBox checkBox;
        Button btSend;
    }
}

logcat的

FATAL EXCEPTION: main
    java.lang.NullPointerException
            at example.com.pocketdocs.Group.ContactListAdapter.getView(ContactListAdapter.java:82)
            at android.widget.AbsListView.obtainView(AbsListView.java:2271)
            at android.widget.ListView.measureHeightOfChildren(ListView.java:1270)
            at android.widget.ListView.onMeasure(ListView.java:1182)
            at android.view.View.measure(View.java:15367)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4924)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1420)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:697)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:578)
            at android.view.View.measure(View.java:15367)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4924)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:314)
            at android.view.View.measure(View.java:15367)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4924)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1420)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:697)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:578)
            at android.view.View.measure(View.java:15367)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4924)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1420)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:697)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:578)
            at android.view.View.measure(View.java:15367)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4924)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:314)
            at android.view.View.measure(View.java:15367)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4924)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1420)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:697)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:578)
            at android.view.View.measure(View.java:15367)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4924)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1420)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:697)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:578)
            at android.view.View.measure(View.java:15367)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4924)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:314)
            at android.view.View.measure(View.java:15367)
            at net.simonvt.menudrawer.OverlayDrawer.onMeasure(OverlayDrawer.java:335)
            at android.view.View.measure(View.java:15367)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4924)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:314)
            at android.view.View.measure(View.java:15367)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4924)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1420)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:697)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:578)
            at android.view.View.measure(View.java:15367)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4924)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:314)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2154)
            at android.view.View.measure(View.java:15367)
            at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1858)
            at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1106)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1279)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4223)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
            at android.view.Choreographer.doCallbacks(Choreographer.java:555)
            at android.view.Choreographer.doFrame(Ch

在该按钮上单击我从数据库中获取一些值

1 个答案:

答案 0 :(得分:0)

NPE in:

holder.btSend.setOnClickListener(...)

由于holder不为null(之前会崩溃),btSend必须为null。这里的NPE没有其他办法了。 btSend初始化为:

holder.btSend = (Button) view.findViewById(R.id.bt_sharecontact_send);

您的contact_custom_list布局不包含ID为bt_sharecontact_send的视图,findViewById()返回null。