Android ButterKnife NullPointer在CustomAdapter中的绑定

时间:2016-12-09 06:02:42

标签: java android butterknife

我尝试在My CustomAdapter类中绑定布局组件,我在我的Holder构造函数上绑定了ButterKnife,在视图组件中绑定了BindView,如下所示:

public static class Holder
{

    public Holder(View view)
    {
        ButterKnife.bind(this,view);
    }

    @BindView(R.id.txtName)
    TextView tvName;

    @BindView(R.id.txtGroup)
    TextView tvGroup;

    @BindView(R.id.imageProfile)
    ImageView img;
}

这是getView方法:

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    Holder holder=new Holder(convertView);
    View rowView;
    rowView = inflater.inflate(R.layout.contact_list, null);

    //holder.tvName=(TextView)rowView.findViewById(R.id.txtName);
    holder.tvName.setText(result.get(position));
    //holder.img=(ImageView) rowView.findViewById(R.id.imageProfile);
    holder.img.setImageBitmap(imageId.get(position));
    //holder.tvGroup=(TextView)rowView.findViewById(R.id.txtGroup);
    holder.tvGroup.setText(listGroup.get(position));
    rowView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Toast.makeText(context, "You Clicked "+result.get(position), Toast.LENGTH_LONG).show();
        }
    });
    return rowView;
}

但是当我运行它时,它会触发NullPointerException,这是消息:

FATAL EXCEPTION: main
                                             Process: com.project.echo.contactmanagement, PID: 2861
                                             java.lang.RuntimeException: Unable to bind views for com.project.echo.contactmanagement.mainscreen.CustomAdapter$Holder
                                                 at butterknife.ButterKnife.bind(ButterKnife.java:189)
                                                 at butterknife.ButterKnife.bind(ButterKnife.java:167)
                                                 at com.project.echo.contactmanagement.mainscreen.CustomAdapter$Holder.<init>(CustomAdapter.java:61)
                                                 at com.project.echo.contactmanagement.mainscreen.CustomAdapter.getView(CustomAdapter.java:76)
                                                 at android.widget.AbsListView.obtainView(AbsListView.java:2263)
                                                 at android.widget.ListView.makeAndAddView(ListView.java:1790)
                                                 at android.widget.ListView.fillDown(ListView.java:691)
                                                 at android.widget.ListView.fillFromTop(ListView.java:752)
                                                 at android.widget.ListView.layoutChildren(ListView.java:1630)
                                                 at android.widget.AbsListView.onLayout(AbsListView.java:2091)
                                                 at android.view.View.layout(View.java:14852)
                                                 at android.view.ViewGroup.layout(ViewGroup.java:4631)
                                                 at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1055)
                                                 at android.view.View.layout(View.java:14852)
                                                 at android.view.ViewGroup.layout(ViewGroup.java:4631)
                                                 at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
                                                 at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
                                                 at android.view.View.layout(View.java:14852)
                                                 at android.view.ViewGroup.layout(ViewGroup.java:4631)
                                                 at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:437)
                                                 at android.view.View.layout(View.java:14852)
                                                 at android.view.ViewGroup.layout(ViewGroup.java:4631)
                                                 at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
                                                 at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
                                                 at android.view.View.layout(View.java:14852)
                                                 at android.view.ViewGroup.layout(ViewGroup.java:4631)
                                                 at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
                                                 at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
                                                 at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
                                                 at android.view.View.layout(View.java:14852)
                                                 at android.view.ViewGroup.layout(ViewGroup.java:4631)
                                                 at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
                                                 at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
                                                 at android.view.View.layout(View.java:14852)
                                                 at android.view.ViewGroup.layout(ViewGroup.java:4631)
                                                 at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1994)
                                                 at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1751)
                                                 at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1007)
                                                 at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5677)
                                                 at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
                                                 at android.view.Choreographer.doCallbacks(Choreographer.java:574)
                                                 at android.view.Choreographer.doFrame(Choreographer.java:544)
                                                 at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
                                                 at android.os.Handler.handleCallback(Handler.java:733)
                                                 at android.os.Handler.dispatchMessage(Handler.java:95)
                                                 at android.os.Looper.loop(Looper.java:136)
                                                 at android.app.ActivityThread.main(ActivityThread.java:5021)
                                                 at java.lang.reflect.Method.invokeNative(Native Method)
                                                 at java.lang.reflect.Method.invoke(Method.java:515)
                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)
                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
                                                 at dalvik.system.NativeStart.main(Native Method)
                                              Caused by: java.lang.NullPointerException
                                                 at butterknife.internal.Finder$1.findView(Finder.java:12)
                                                 at butterknife.internal.Finder.findOptionalView(Finder.java:64)
                                                 at butterknife.internal.Finder.findRequiredView(Finder.java:48)
                                                 at com.project.echo.contactmanagement.mainscreen.CustomAdapter$Holder$$ViewBinder.bind(CustomAdapter$Holder$$ViewBinder.java:17)
                                                 at com.project.echo.contactmanagement.mainscreen.CustomAdapter$Holder$$ViewBinder.bind(CustomAdapter$Holder$$ViewBinder.java:12)
                                                 at butterknife.ButterKnife.bind(ButterKnife.java:187)
                                                 at butterknife.ButterKnife.bind(ButterKnife.java:167) 
                                                 at com.project.echo.contactmanagement.mainscreen.CustomAdapter$Holder.<init>(CustomAdapter.java:61) 
                                                 at com.project.echo.contactmanagement.mainscreen.CustomAdapter.getView(CustomAdapter.java:76) 
                                                 at android.widget.AbsListView.obtainView(AbsListView.java:2263) 
                                                 at android.widget.ListView.makeAndAddView(ListView.java:1790) 
                                                 at android.widget.ListView.fillDown(ListView.java:691) 
                                                 at android.widget.ListView.fillFromTop(ListView.java:752) 
                                                 at android.widget.ListView.layoutChildren(ListView.java:1630) 
                                                 at android.widget.AbsListView.onLayout(AbsListView.java:2091) 
                                                 at android.view.View.layout(View.java:14852) 
                                                 at android.view.ViewGroup.layout(ViewGroup.java:4631) 
                                                 at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1055) 
                                                 at android.view.View.layout(View.java:14852) 
                                                 at android.view.ViewGroup.layout(ViewGroup.java:4631) 
                                                 at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453) 
                                                 at android.widget.FrameLayout.onLayout(FrameLayout.java:388) 
                                                 at android.view.View.layout(View.java:14852) 
                                                 at android.view.ViewGroup.layout(ViewGroup.java:4631) 
                                                 at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:437) 
                                                 at android.view.View.layout(View.java:14852) 
                                                 at android.view.ViewGroup.layout(ViewGroup.java:4631) 
                                                 at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453) 
                                                 at android.widget.FrameLayout.onLayout(FrameLayout.java:388) 
                                                 at android.view.View.layout(View.java:14852) 
                                                 at android.view.ViewGroup.layout(ViewGroup.java:4631) 
                                                 at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671) 
                                                 at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525) 
                                                 at android.widget.LinearLayout.onLayout(LinearLayout.java:1434) 
                                                 at android.view.View.layout(View.java:14852) 
                                                 at android.view.ViewGroup.layout(ViewGroup.java:4631) 
                                                 at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453) 
                                                 at android.widget.FrameLayout.onLayout(FrameLayout.java:388) 
                                                 at android.view.View.layout(View.java:14852) 
                                                 at android.view.ViewGroup.layout(ViewGroup.java:4631) 
                                                 at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1994) 
                                                 at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1751) 
                                                 at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1007) 
                                                 at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5677) 
                                                 at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761) 
                                                 at android.view.Choreographer.doCallbacks(Choreographer.java:574) 
                                                 at android.view.Choreographer.doFrame(Choreographer.java:544) 
                                                 at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747) 
                                                 at android.os.Handler.handleCallback(Handler.java:733) 
                                                 at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                 at android.os.Looper.loop(Looper.java:136) 
                                                 at android.app.ActivityThread.main(ActivityThread.java:5021) 
                                                 at java.lang.reflect.Method.invokeNative(Native Method) 
                                                 at java.lang.reflect.Method.invoke(Method.java:515) 
                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827) 
                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643) 
                                                 at dalvik.system.NativeStart.main(Native Method) 

请有人帮我解决这个问题吗?

3 个答案:

答案 0 :(得分:2)

由于convertView为null,而且您无需创建RowView,只需对convertView进行充气。

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    final Holder holder;
    if (null == convertView) {
        convertView = inflater.inflate(R.layout.contact_list, null);
        holder = new Holder(convertView);
        convertView.setTag(holder);
    } else {
        holder = (Holder) convertView.getTag();
    }

    //holder.tvName=(TextView)rowView.findViewById(R.id.txtName);
    holder.tvName.setText(result.get(position));
    //holder.img=(ImageView) rowView.findViewById(R.id.imageProfile);
    holder.img.setImageBitmap(imageId.get(position));
    //holder.tvGroup=(TextView)rowView.findViewById(R.id.txtGroup);
    holder.tvGroup.setText(listGroup.get(position));
    rowView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Toast.makeText(context, "You Clicked "+result.get(position), Toast.LENGTH_LONG).show();
        }
    });
    return convertView;
}

但我建议您使用RecyclerView替换ListViewRecyclerView创建ViewHolder并且无需创建ViewHolder。

  

REF:https://developer.android.com/reference/android/support/v7/widget/RecyclerView.html

答案 1 :(得分:2)

ButterKnife.bind(this,convertView);

答案 2 :(得分:0)

是的,我认为是因为convertView为null,我将我的代码更改为此,它最终有效,谢谢大家的帮助:

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    View rowView;
    rowView = inflater.inflate(R.layout.contact_list, null);
    Holder holder=new Holder(rowView);

    holder.tvName.setText(result.get(position));
    holder.img.setImageBitmap(imageId.get(position));
    holder.tvGroup.setText(listGroup.get(position));
    rowView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Toast.makeText(context, "You Clicked "+result.get(position), Toast.LENGTH_LONG).show();
        }
    });
    return rowView;
}