1

我的 ListView 代码有点问题,它应该显示尚未与设备配对的可用设备列表。

我已包含以下错误:

FATAL EXCEPTION: main
at com.zephyr.bolt.UnpairedDeviceViewer$StableArrayAdapter.getItemId(UnpairedDeviceViewer.java:110)
at android.widget.AbsListView.obtainView(AbsListView.java:2198)

(遗憾的是,我没有包括整个错误,错误在我的平板电脑上,我似乎无法从 AIDE 中复制出来)

下面是引用的类文件,它是 XML 文件

UnpairedDeviceViewer.java

package com.zephyr.bolt;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

public class UnpairedDeviceViewer extends Activity{
    Activity a = this;
    private ListView listview;
    private Button b1;
    public StableArrayAdapter adapter;
    private ArrayList strings;
    private BroadcastReceiver recv;

protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.udv);

    b1 = (Button) findViewById(R.id.b5);

    listview = (ListView) findViewById(R.id.l2);

    strings = new ArrayList<String>();

    adapter = new StableArrayAdapter(this, android.R.layout.simple_list_item_1, strings);

    recv = new BroadcastReceiver() {

        @Override
        public void onReceive(Context arg0, Intent arg1) {
            String action = arg1.getAction();

            if(BluetoothDevice.ACTION_FOUND.equals(action))
            {
                BluetoothDevice device = arg1.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

                adapter.add(device.getName() + "\n" + device.getAddress());
                adapter.notifyDataSetChanged();

            }
            else if(BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action))
            {
                Toast.makeText(a, "Device Discovery Started", Toast.LENGTH_LONG).show();
            }
            else if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action))
            {
                Toast.makeText(a, "Device Discovery Completed", Toast.LENGTH_LONG).show();
            }

        }

    };

    IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
    filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
    filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
    registerReceiver(recv, filter);

    listview.setAdapter(adapter);

    b1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            MainActivity.bTAdapter.startDiscovery();

        }
    });


}

protected void onDestroy()
{
    MainActivity.bTAdapter.cancelDiscovery();
    unregisterReceiver(recv);
}


private class StableArrayAdapter extends ArrayAdapter<String> {

    HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();

    public StableArrayAdapter(Context context, int textViewResourceId,
                              List<String> objects) {
        super(context, textViewResourceId, objects);
        for (int i = 0; i < objects.size(); ++i) {
            mIdMap.put(objects.get(i), i);
        }
    }

    @Override
    public long getItemId(int position) {
        String item = getItem(position);
        return mIdMap.get(item);
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

}

}

udv.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android" >
    <Button 
        android:id = "@+id/b5"
        android:layout_centerHorizontal="true"
        android:layout_width = "match_parent"
        android:layout_height = "wrap_content"
        android:text = "Begin Discovery"
        />  
    <ListView 
        android:id = "@+id/l2"
        android:layout_below="@+id/b5"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </ListView>
</RelativeLayout>
4

1 回答 1

1

mIdMap是一种过于复杂的方式来做ArrayAdapter已经做的事情。其执行getItemId(pos)退货pos

请注意,您只构建mIdMap一次,您在构建后添加的任何项目(如在接收器回调中)都没有 id,并且会导致您的getItemId实现中的 NPE 尝试 unbox null

另外,调用notifyDataSetChanged()是不必要的,默认情况下ArrayAdapter会在每次调用之后自动调用它add()

于 2013-11-02T02:57:33.890 回答