Android Studio - 无法解析数组适配器中的符号项

时间:2015-01-07 20:26:49

标签: java android bluetooth android-arrayadapter

我目前正在尝试使用一些不同的教程在Android应用程序中实现蓝牙连接,并且遇到了尝试将检测到的配对设备添加到ListView以供用户选择的问题。

目前正在下面的行中抛出错误“无法解析符号项”,但我不知道用什么替换项目或者如果这是唯一的问题。 itemsAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,items);

如果有人可以在Java代码中更正我的语法,我将非常感激。

谢谢!

Java代码

package com.example.khite.wheelnav;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.Set;

public class ConnectActivity extends ActionBarActivity {

    private static final int REQUEST_ENABLE_BT = 1234;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_connect);

        // Create Bluetooth Adapter
        BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if (mBluetoothAdapter == null) {
                Intent intent;
                intent = new Intent(this, NoBluetoothActivity.class);
                startActivity(intent);
        }

        // Enable Bluetooth
        if (!mBluetoothAdapter.isEnabled()) {
            Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
        }

        // Create an Array Adapter to Add Devices to
        ArrayAdapter<String> itemsAdapter;
        itemsAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);

        //Query Paired Devices
        Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
        // If there are paired devices
        if (pairedDevices.size() > 0) {
            // Loop through paired devices
            for (BluetoothDevice device : pairedDevices) {
                // Add the name and address to an array adapter to show in a ListView
                itemsAdapter.add(device.getName() + "\n" + device.getAddress());
            }
        }

        // Add Paired Devices to a List View
        ListView listView = (ListView) findViewById(R.id.listView);
        listView.setAdapter(itemsAdapter);
    }

    public void openChooseFunctionActivity(View view){
        //open choose function activity
            Intent intent;
            intent = new Intent(this, ChooseFunctionActivity.class);
            startActivity(intent);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

XML代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.example.khite.wheelnav.ConnectActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Continue"
        android:id="@+id/button2"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:onClick="openChooseFunctionActivity"
        android:background="#ff8180fd"
        android:textColor="#ff000000" />

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/listView"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

2 个答案:

答案 0 :(得分:0)

代码中的

“items”根本没有初始化。您必须传递String []或List&lt; String&gt; object作为ArrayAdapter构造函数的最后一个参数。

“无法解析符号x”通常表示无法找到变量x。

答案 1 :(得分:0)

首先初始化您的项目集合,然后初始化适配器

            //Query Paired Devices
            Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
            // If there are paired devices
            List<String> items = new ArrayList<String>();
            if (pairedDevices.size() > 0) {
                // Loop through paired devices
                for (BluetoothDevice device : pairedDevices) {
                    // Add the name and address to an array adapter to show in a ListView
                    items.add(device.getName() + "\n" + device.getAddress());
                }
            }

           // Create an Array Adapter to Add Devices to
            ArrayAdapter<String> itemsAdapter;
            itemsAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);