java Android Studio - 无法解析数组适配器中的符号项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27828053/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android Studio - Cannot Resolve Symbol items in Array Adapter
提问by kghite
I am currently trying to implement a Bluetooth connection in an Android application using a few different tutorials and am running into issues trying to add detected paired devices to a ListView for the user to choose from.
我目前正在尝试使用一些不同的教程在 Android 应用程序中实现蓝牙连接,并且在尝试将检测到的配对设备添加到 ListView 以供用户选择时遇到问题。
It is currently throwing the error "Cannot resolve symbol items" in the line below, but I'm not sure what to replace items with or if that's the only issue. itemsAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, items);
它目前在下面的行中抛出错误“无法解析符号项目”,但我不确定用什么替换项目,或者这是唯一的问题。itemsAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, items);
I would really appreciate it if someone could correct my syntax in the Java code.
如果有人可以更正我在 Java 代码中的语法,我将不胜感激。
Thanks!
谢谢!
Java Code
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 Code
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>
采纳答案by SorryForMyEnglish
initialized first your items collection, then an adapter
首先初始化您的项目集合,然后是适配器
//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);
回答by Longi
"items" in your code is not initalized at all. You have to pass a String[] or List<String> object as the last parameter of the ArrayAdapterconstructor.
代码中的“items”根本没有初始化。您必须将 String[] 或 List<String> 对象作为ArrayAdapter构造函数的最后一个参数传递。
"Cannot resolve symbol x" generally means variable x cannot be found.
“无法解析符号 x”通常意味着无法找到变量 x。