-1

My problem is getTag() is invoke on a null method. Say in the logcat below.( My app already have setTag() then getTag().

I have an adapter code like this for ListView :

public BeaconListAdapter(Activity context){
        this.inflater = LayoutInflater.from(context);
        this.beacons = new ArrayList<>();
    }

    public void replaceWith(Collection<com.estimote.coresdk.recognition.packets.Beacon> newBeacons){
        this.beacons.clear();
        this.beacons.addAll(newBeacons);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return beacons.size();
    }

    @Override
    public com.estimote.coresdk.recognition.packets.Beacon getItem(int position) {
        return beacons.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        convertView = inflateRequires(convertView);
        bind(getItem(position),convertView);
        return convertView;
    }

    private void bind(final com.estimote.coresdk.recognition.packets.Beacon beacon, View view){
        final ViewHolder viewHolder = (ViewHolder) view.getTag();
        viewHolder.Address.setText(String.format("Adress:%s", beacon.getMacAddress().toStandardString()));
        viewHolder.Distance.setText(String.format("Distance is : %f",com.estimote.coresdk.observation.region.RegionUtils.computeAccuracy(beacon)));

    }

    private View inflateRequires(View view){
        if (view == null){
            view = inflater.inflate(R.layout.view_beacon_info,null,false);
            view.setTag(new ViewHolder(view));
        }
        return null;
    }

    static class ViewHolder{
        public TextView Distance;
        public TextView Address;

        ViewHolder(View view){
            Distance = (TextView) view.findViewById(R.id.Txt1);
            Address = (TextView) view.findViewById(R.id.Txt2);
        }
    }
}

And my app keep crashing down from the beginging. The log is like this:

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.user.beaconranging, PID: 5820
                  java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.view.View.getTag()' on a null object reference
                      at com.example.user.beaconranging.Adapter.BeaconListAdapter.bind(BeaconListAdapter.java:59)
                      at com.example.user.beaconranging.Adapter.BeaconListAdapter.getView(BeaconListAdapter.java:54)
                      at android.widget.AbsListView.obtainView(AbsListView.java:2428)
                      at android.widget.ListView.makeAndAddView(ListView.java:2083)
                      at android.widget.ListView.fillDown(ListView.java:793)
                      at android.widget.ListView.fillFromTop(ListView.java:859)
                      at android.widget.ListView.layoutChildren(ListView.java:1816)
                      at android.widget.AbsListView.onLayout(AbsListView.java:2226)
                      at android.view.View.layout(View.java:19781)
                      at android.view.ViewGroup.layout(ViewGroup.java:6144)
                      at android.widget.FrameLayout.layoutChildren(FrameLayout.java:325)
                      at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
                      at android.view.View.layout(View.java:19781)
                      at android.view.ViewGroup.layout(ViewGroup.java:6144)
                      at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:443)
                      at android.view.View.layout(View.java:19781)
                      at android.view.ViewGroup.layout(ViewGroup.java:6144)
                      at android.widget.FrameLayout.layoutChildren(FrameLayout.java:325)
                      at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
                      at android.view.View.layout(View.java:19781)
                      at android.view.ViewGroup.layout(ViewGroup.java:6144)
                      at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1816)
                      at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1660)
                      at android.widget.LinearLayout.onLayout(LinearLayout.java:1569)
                      at android.view.View.layout(View.java:19781)
                      at android.view.ViewGroup.layout(ViewGroup.java:6144)
                      at android.widget.FrameLayout.layoutChildren(FrameLayout.java:325)
                      at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
                      at com.android.internal.policy.DecorView.onLayout(DecorView.java:888)
                      at android.view.View.layout(View.java:19781)
                      at android.view.ViewGroup.layout(ViewGroup.java:6144)
                      at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2653)
                      at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2356)
                      at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1512)
                      at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7218)
                      at android.view.Choreographer$CallbackRecord.run(Choreographer.java:981)
                      at android.view.Choreographer.doCallbacks(Choreographer.java:790)
                      at android.view.Choreographer.doFrame(Choreographer.java:721)
                      at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:967)
                      at android.os.Handler.handleCallback(Handler.java:808)
                      at android.os.Handler.dispatchMessage(Handler.java:101)
                      at android.os.Looper.loop(Looper.java:166)
                      at android.app.ActivityThread.main(ActivityThread.java:7425)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)

I see the logcat and it's fault is in the code i up above, on line 59 and line 54.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
TMN167
  • 112
  • 1
  • 8

1 Answers1

5

you see this method is always returning null. so your convertView is always null.

 private View inflateRequires(View view){
        if (view == null){
            view = inflater.inflate(R.layout.view_beacon_info,null,false);
            view.setTag(new ViewHolder(view));
        }
        return null;
    }

change it to :-

private View inflateRequires(View view){
            if (view == null){
                view = inflater.inflate(R.layout.view_beacon_info,null,false);
                view.setTag(new ViewHolder(view));
            }
            return view;
        }
r4jiv007
  • 2,974
  • 3
  • 29
  • 36