Java Android Studio-无法解析阵列适配器中的符号项
我目前正在尝试使用一些不同的教程在Android应用程序中实现蓝牙连接,并且在尝试将检测到的配对设备添加到ListView以供用户选择时遇到了问题 它目前在下面的行中抛出错误“无法解析符号项”,但我不确定用什么替换项,或者这是否是唯一的问题。 itemsAdapter=newarrayadapter(这是android.R.layout.simple\u list\u item\u 1,items) 如果有人能在Java代码中纠正我的语法,我将不胜感激 谢谢 Java代码Java Android Studio-无法解析阵列适配器中的符号项,java,android,bluetooth,android-arrayadapter,Java,Android,Bluetooth,Android Arrayadapter,我目前正在尝试使用一些不同的教程在Android应用程序中实现蓝牙连接,并且在尝试将检测到的配对设备添加到ListView以供用户选择时遇到了问题 它目前在下面的行中抛出错误“无法解析符号项”,但我不确定用什么替换项,或者这是否是唯一的问题。 itemsAdapter=newarrayadapter(这是android.R.layout.simple\u list\u item\u 1,items) 如果有人能在Java代码中纠正我的语法,我将不胜感激 谢谢 Java代码 package com
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);
}
}
package com.example.khite.wheelnav;
导入android.content.Intent;
导入android.support.v7.app.ActionBarActivity;
导入android.os.Bundle;
导入android.view.MenuItem;
导入android.view.view;
导入android.bluetooth.BluetoothAdapter;
导入android.bluetooth.bluetooth设备;
导入android.widget.ArrayAdapter;
导入android.widget.ListView;
导入java.util.Set;
公共类ConnectActivity扩展了ActionBarActivity{
私有静态最终整数请求\启用\ BT=1234;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_connect);
//创建蓝牙适配器
BluetoothAdapter mBluetoothAdapter=BluetoothAdapter.getDefaultAdapter();
if(mBluetoothAdapter==null){
意图;
intent=新的intent(this,NoBluetoothActivity.class);
星触觉(意向);
}
//启用蓝牙
如果(!mBluetoothAdapter.isEnabled()){
Intent enablebintent=新意图(BluetoothAdapter.ACTION\u REQUEST\u ENABLE);
startActivityForResult(启用BTIntent、请求\启用\ BT);
}
//创建阵列适配器以将设备添加到
ArrayAdapter项Adapter;
itemsAdapter=newarrayadapter(这是android.R.layout.simple\u list\u item\u 1,items);
//查询配对设备
设置pairedDevices=mBluetoothAdapter.getBondedDevices();
//如果有配对的设备
如果(pairedDevices.size()>0){
//循环通过配对设备
用于(蓝牙设备:pairedDevices){
//将名称和地址添加到要在列表视图中显示的阵列适配器
itemsAdapter.add(device.getName()+“\n”+device.getAddress());
}
}
//将配对设备添加到列表视图
ListView ListView=(ListView)findViewById(R.id.ListView);
setAdapter(itemsAdapter);
}
public void openChooseFunctionActivity(视图){
//打开选择功能活动
意图;
intent=新intent(选择此项,选择functionactivity.class);
星触觉(意向);
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
//处理操作栏项目单击此处。操作栏将
//自动处理Home/Up按钮上的点击,只要
//在AndroidManifest.xml中指定父活动时。
int id=item.getItemId();
//noinspection SimplifiableIf语句
if(id==R.id.action\u设置){
返回true;
}
返回super.onOptionsItemSelected(项目);
}
}
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>
代码中的“项”根本没有初始化。必须传递字符串[]或列表对象作为构造函数的最后一个参数
“无法解析符号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);
//查询配对设备
设置pairedDevices=mBluetoothAdapter.getBondedDevices();
//如果有配对的设备
列表项=新建ArrayList();
如果(pairedDevices.size()>0){
//循环通过配对设备
用于(蓝牙设备:pairedDevices){
//将名称和地址添加到要在列表视图中显示的阵列适配器
添加(device.getName()+“\n”+device.getAddress());
}
}
//创建阵列适配器以将设备添加到
ArrayAdapter项Adapter;
itemsAdapter=newarrayadapter(这是android.R.layout.simple\u list\u item\u 1,items);
成功了,谢谢!我添加了List=newarraylist();在将设备添加到阵列适配器之前,使用空列表对象初始化阵列适配器。