应用程序在尝试构建和运行时崩溃

时间:2018-05-27 20:29:49

标签: java android nullpointerexception

我的问题是getTag()是在null方法上调用的。在下面的logcat中说。(我的应用已经有setTag(),然后是getTag()。

我有一个适用于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);
        }
    }
}

我的应用程序不断从崩溃中崩溃。日志是这样的:

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)

我看到了logcat,它的错误出现在上面的代码中,在第59行和第54行。

1 个答案:

答案 0 :(得分:3)

你看到这个方法总是返回null。所以你的convertView始终为空。

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

将其更改为: -

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