-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 内のすべての Bluetooth デバイスを表示します。しかし、このコードを試すと、次の例外が発生します。

 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 のテキストビューのみ

<?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はテキストビューリソースIDのみを受け取ります..

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

ここで、textview リソース ID を渡す必要があります.. のように..

 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 に関するドキュメント

public ArrayAdapter (コンテキスト コンテキスト、int リソース、int textViewResourceId)

コンストラクタ

パラメータ
context 現在のコンテキスト。

resource
ビューをインスタンス化するときに使用するレイアウトを含むレイアウト ファイルのリソース ID。

textViewResourceId設定されるレイアウト リソース内のTextView
の ID

RESULT_CANCELEDレイアウト内の TextView の ID ではないようです。また、アクティビティのコンテンツ ビューを設定したのと同じレイアウトを渡していますが、これも間違っている可能性があります。

詳細については、アダプターを使用したレイアウトの構築をお読みになることをお勧めします。

于 2013-11-04T07:03:44.550 に答える