从默认Android BLE适配器示例删除Inflate视图[java]

时间:2015-04-16 08:21:51

标签: java android list android-inflate

我对来自官方Android网站的BLE适配器的默认示例感到安静。 但我不知道如何从UI中删除所有设备列表。 我不想显示列表中找到的设备,但我不知道如何删除Inflate。 删除总是为我生成NullPointerException。 如果有人能告诉我如何从布局中删除膨胀的视图列表并且只是在bAckground中保存设备列表,将非常感激

// Adapter for holding devices found through scanning.
private class LeDeviceListAdapter extends BaseAdapter {
    private ArrayList<BluetoothDevice> mLeDevices;
    private LayoutInflater mInflator;



    public LeDeviceListAdapter() {
        super();
        mLeDevices = new ArrayList<BluetoothDevice>();

         mInflator = DeviceScanActivity.this.getLayoutInflater();


    }

    public void addDevice(BluetoothDevice device) {
        if(!mLeDevices.contains(device)) {
            mLeDevices.add(device);

        }
    }

    public BluetoothDevice getDevice(int position) {
        return mLeDevices.get(position);
    }

    public void clear() {
        mLeDevices.clear();
    }

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

    @Override
    public Object getItem(int i) {
        return mLeDevices.get(i);
    }

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

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




        ViewHolder viewHolder;
        // General ListView optimization code.
        if (view == null) {
            view = mInflator.inflate(R.layout.listitem_device, null);
            viewHolder = new ViewHolder();
            //viewHolder.deviceAddress = (TextView) view.findViewById(R.id.device_address);
            // viewHolder.deviceName = (TextView) view.findViewById(R.id.device_name);
            //  viewHolder.deviceUUID = (TextView) view.findViewById(R.id.device_uuid);
           // view.setTag(viewHolder);

        } else {
           // viewHolder = (ViewHolder) view.getTag();
        }

如果我删除了inflater Lines

,那就是崩溃日志
04-16 11:12:49.145  31181-31181/com.example.android.bluetoothlegatt E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.android.bluetoothlegatt, PID: 31181
java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.View.getImportantForAccessibility()' on a null object reference
        at android.widget.AbsListView.obtainView(AbsListView.java:2483)
        at android.widget.ListView.makeAndAddView(ListView.java:1864)
        at android.widget.ListView.fillDown(ListView.java:698)
        at android.widget.ListView.fillFromTop(ListView.java:759)
        at android.widget.ListView.layoutChildren(ListView.java:1659)
        at android.widget.AbsListView.onLayout(AbsListView.java:2271)
        at android.view.View.layout(View.java:15695)
        at android.view.ViewGroup.layout(ViewGroup.java:4981)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
        at android.view.View.layout(View.java:15695)
        at android.view.ViewGroup.layout(ViewGroup.java:4981)
        at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:494)
        at android.view.View.layout(View.java:15695)
        at android.view.ViewGroup.layout(ViewGroup.java:4981)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
        at android.view.View.layout(View.java:15695)
        at android.view.ViewGroup.layout(ViewGroup.java:4981)
        at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2186)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1920)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1106)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6018)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:792)
        at android.view.Choreographer.doCallbacks(Choreographer.java:596)
        at android.view.Choreographer.doFrame(Choreographer.java:557)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:778)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:155)
        at android.app.ActivityThread.main(ActivityThread.java:5696)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)

0 个答案:

没有答案