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

Android Studio - Cannot Resolve Symbol items in Array Adapter

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

它目前在下面的行中抛出错误 "Cannot resolve symbol items",但我不确定用什么替换项目,或者这是否是唯一的问题。 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>

"items" 在您的代码中根本没有初始化。您必须传递一个 String[] 或 List 对象作为 ArrayAdapter 构造函数的最后一个参数。

"Cannot resolve symbol x"一般表示找不到变量x。

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

            //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);