-1
 public class DeviceListActivity extends Activity {
        protected static final String TAG = "TAG";
        private BluetoothAdapter mBluetoothAdapter;
        private ArrayAdapter<String> mPairedDevicesArrayAdapter;

        @Override
        protected void onCreate(Bundle mSavedInstanceState) {
            super.onCreate(mSavedInstanceState);
            requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
            setContentView(R.layout.activity_device_list);

            setResult(Activity.RESULT_CANCELED);
            mPairedDevicesArrayAdapter = new ArrayAdapter<String>(this,
                    R.layout.activity_device_list, RESULT_CANCELED);

            ListView mPairedListView = (ListView) findViewById(R.id.paired_devices);
            mPairedListView.setAdapter(mPairedDevicesArrayAdapter);
            mPairedListView.setOnItemClickListener(mDeviceClickListener);

            mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
            Set<BluetoothDevice> mPairedDevices = mBluetoothAdapter
                    .getBondedDevices();

            if (mPairedDevices.size() > 0) {
                // findViewById(R.id.title_paired_devices).setVisibility(View.VISIBLE);
                for (BluetoothDevice mDevice : mPairedDevices) {
                    mPairedDevicesArrayAdapter.add(mDevice.getName() + "\n"
                            + mDevice.getAddress());
                }
            } else {
                String mNoDevices = "None Paired";// getResources().getText(R.string.none_paired).toString();
                mPairedDevicesArrayAdapter.add(mNoDevices);
            }
        }

        @Override
        protected void onDestroy() {
            super.onDestroy();
            if (mBluetoothAdapter != null) {
                mBluetoothAdapter.cancelDiscovery();
            }
        }

        private OnItemClickListener mDeviceClickListener = new OnItemClickListener() {
            public void onItemClick(AdapterView<?> mAdapterView, View mView,
                    int mPosition, long mLong) {
                mBluetoothAdapter.cancelDiscovery();
                String mDeviceInfo = String.valueOf(mPairedDevicesArrayAdapter
                        .getItem(mPosition).toString());
                String mDeviceAddress = mDeviceInfo
                        .substring(mDeviceInfo.length() - 17);
                Log.v(TAG, "Device_Address " + mDeviceAddress);

                Bundle mBundle = new Bundle();
                mBundle.putString("DeviceAddress", mDeviceAddress);
                Intent mBackIntent = new Intent();
                mBackIntent.putExtras(mBundle);
                setResult(Activity.RESULT_OK, mBackIntent);
                finish();
            }
        };

    }

device_list.xml

 <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/title_paired_devices"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="#666"
            android:paddingLeft="5dip"
            android:text="My Text"
            android:textColor="#fff"
            android:visibility="gone" />

        <ListView
            android:id="@+id/paired_devices"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:stackFromBottom="true" />

    </LinearLayout>

我编写上面的代码来显示当前在线的 ListView 中的所有蓝牙设备。但是当我尝试此代码时,我得到以下异常。此代码在不使用阵列适配器的情况下工作正常

 11-04 12:03:48.504: E/AndroidRuntime(8902): java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
4

3 回答 3

1

改变这个

mPairedDevicesArrayAdapter = new ArrayAdapter<String>(this,
                R.layout.activity_device_list, RESULT_CANCELED);

mPairedDevicesArrayAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, RESULT_CANCELED);

android.R.layout.simple_list_tiem1以上使用列表视图的内置布局

11-04 12:03:48.504: E/AndroidRuntime(8902): java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView

您的适配器参数错误。

或者

mPairedDevicesArrayAdapte = new ArrayAdapter<String>(this, R.layout.list_item, RESULT_CANCELED);

list_item.xml// 只有xml中的textview

<?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="test"
    />

或者

    mPairedDevicesArrayAdapte= new ArrayAdapter<String>(this, R.layout.list_item,R.id.textView1, RESULT_CANCELED);

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dip" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="test"
    />

</RelativeLayout>
于 2013-11-04T07:04:15.143 回答
0

在这里,您传递了您的布局 ID,但 arrayadapter 仅采用了 textview 资源 ID。

 mPairedDevicesArrayAdapter = new ArrayAdapter<String>(this,
                R.layout.activity_device_list, RESULT_CANCELED);

在这里你必须传递 textview 资源 id .. like ..

 mPairedDevicesArrayAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, RESULT_CANCELED);
于 2013-11-04T07:03:42.087 回答
0

如错误所述ArrayAdapter requires the resource ID to be a TextView

ArrayAdapter 上的文档

公共 ArrayAdapter(上下文上下文,int 资源,int textViewResourceId)

构造函数

参数
context 当前上下文。

resource
包含在实例化视图时使用的布局的布局文件的资源 ID。

textViewResourceId要填充的布局资源中TextView
的 id

RESULT_CANCELED似乎不是布局中 TextView 的 ID。您还向它传递了与设置活动的内容视图相同的布局,这也可能是不正确的。

我建议阅读Building Layouts with a Adapter了解更多信息。

于 2013-11-04T07:03:44.550 回答