我一直在研究这个蓝牙数据传输项目已有一段时间了,现在我已经完成了它,我不断在另一个错误旁边遇到这个恼人的错误nonStop
错误:(41,35)找不到与给定名称匹配的资源(在'layout_toRightOf'中,值为'@id / edttxt')。
任何想法都存在问题?
活动主要
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:background="@drawable/mybg"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.cy.el.bluetoothtransfer.MainActivity">
<Button
android:text="ON/OFF"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:background="@drawable/selector2"
android:layout_alignParentRight="true"
android:id="@+id/btnONOFF"/>
<Button
android:text="Enable Discoverable"
android:layout_width="189dp"
android:layout_height="50dp"
android:background="@drawable/selector"
android:id="@+id/btnDiscoverable_on_off"
android:onClick="btnEnableDisable_Discoverable"
android:layout_alignParentTop="true" />
<Button
android:layout_width="95dp"
android:layout_height="50dp"
android:id="@+id/btnFindUnpairedDevices"
android:text="Discover"
android:layout_toRightOf="@id/edttxt"
android:layout_marginLeft="50dp"
android:layout_above="@id/edttxt"
android:layout_marginTop="40dp"
android:background="@drawable/selector"
android:onClick="btnDiscover"/>
<ListView
android:layout_marginTop="120dp"
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="@+id/lvNewDevices"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/lvNewDevices"
android:layout_marginTop="30dp"
android:layout_alignParentLeft="true"
android:textSize="20dp"
android:layout_marginLeft="30dp"
android:textColor="#fffffe"
android:id="@+id/information" />
<EditText
android:layout_width="200dp"
android:layout_height="100dp"
android:layout_below="@id/information"
android:hint=" Type here : "
android:textColor="#fffffe"
android:background="#fafafa"
android:layout_marginTop="50dp"
android:id="@+id/edttxt"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/information"
android:layout_toRightOf="@id/edttxt"
android:text="Send"
android:background="@drawable/selector2"
android:layout_marginTop="50dp"
android:layout_marginLeft="50dp"
android:id="@+id/sendBtn"/>
<Button
android:text="Start Connection"
android:id="@+id/btnstartConnection"
android:layout_width="189dp"
android:layout_height="50dp"
android:layout_below="@id/btnDiscoverable_on_off"
android:background="@drawable/selector"/>
</RelativeLayout>
MainActivity
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener{
BluetoothConnection myBluetoothConnection ;
private static final UUID myUUID = UUID.fromString("8ce255c0-200a-11e0-ac64-0800200c9a66");
BluetoothDevice myBluetoothDevice ;
TextView informations ;
BluetoothAdapter mBluetoothAdapter;
Button btnEnableDisable_Discoverable;
EditText edt ;
Button btnSend ;
Button btnstartconnection ;
public ArrayList<BluetoothDevice> mBTDevices = new ArrayList<>();
public deviceList mDeviceListAdapter;
ListView lvNewDevices;
// Create a BroadcastReceiver for ACTION_FOUND
private final BroadcastReceiver mBroadcastReceiver1 = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
// When discovery finds a device
if (action.equals(mBluetoothAdapter.ACTION_STATE_CHANGED)) {
final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, mBluetoothAdapter.ERROR);
switch(state){
case BluetoothAdapter.STATE_OFF:
break;
case BluetoothAdapter.STATE_TURNING_OFF:
Toast.makeText(getApplicationContext(), " Turning Off ", Toast.LENGTH_SHORT).show();
break;
case BluetoothAdapter.STATE_ON:
break;
case BluetoothAdapter.STATE_TURNING_ON:
Toast.makeText(getApplicationContext(), " Turning On ", Toast.LENGTH_SHORT).show();
break;
}
}
}
};
/**
* Broadcast Receiver for changes made to bluetooth states such as:
* 1) Discoverability mode on/off or expire.
*/
private final BroadcastReceiver mBroadcastReceiver2 = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (action.equals(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED)) {
int mode = intent.getIntExtra(BluetoothAdapter.EXTRA_SCAN_MODE, BluetoothAdapter.ERROR);
switch (mode) {
//Device is in Discoverable Mode
case BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE:
break;
//Device not in discoverable mode
case BluetoothAdapter.SCAN_MODE_CONNECTABLE:
break;
case BluetoothAdapter.SCAN_MODE_NONE:
break;
case BluetoothAdapter.STATE_CONNECTING:
Toast.makeText(getApplicationContext(), " Connecting... ", Toast.LENGTH_SHORT).show();
break;
case BluetoothAdapter.STATE_CONNECTED:
Toast.makeText(getApplicationContext(), " Connected ", Toast.LENGTH_SHORT).show();
break;
}
}
}
};
/**
* Broadcast Receiver for listing devices that are not yet paired
* -Executed by btnDiscover() method.
*/
private BroadcastReceiver mBroadcastReceiver3 = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (action.equals(BluetoothDevice.ACTION_FOUND)){
BluetoothDevice device = intent.getParcelableExtra (BluetoothDevice.EXTRA_DEVICE);
mBTDevices.add(device);
informations.setText(device.getName() + " : " + device.getAddress());
mDeviceListAdapter = new deviceList(context, R.layout.devicelistview, mBTDevices);
lvNewDevices.setAdapter(mDeviceListAdapter);
}
}
};
/**
* Broadcast Receiver that detects bond state changes (Pairing status changes)
*/
private final BroadcastReceiver mBroadcastReceiver4 = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if(action.equals(BluetoothDevice.ACTION_BOND_STATE_CHANGED)){
BluetoothDevice mDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
//3 cases:
//case1: bonded already
if (mDevice.getBondState() == BluetoothDevice.BOND_BONDED){
Toast.makeText(getApplicationContext(), " Bonded ", Toast.LENGTH_SHORT).show();
myBluetoothDevice = mDevice ;
}
//case2: creating a bone
if (mDevice.getBondState() == BluetoothDevice.BOND_BONDING) {
Toast.makeText(getApplicationContext(), " Bonding ", Toast.LENGTH_SHORT).show();
}
//case3: breaking a bond
if (mDevice.getBondState() == BluetoothDevice.BOND_NONE) {
Toast.makeText(getApplicationContext(), " Not Bonded ", Toast.LENGTH_SHORT).show(); }
}
}
};
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(mBroadcastReceiver1);
unregisterReceiver(mBroadcastReceiver2);
unregisterReceiver(mBroadcastReceiver3);
unregisterReceiver(mBroadcastReceiver4);
//mBluetoothAdapter.cancelDiscovery();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnONOFF = (Button) findViewById(R.id.btnONOFF);
btnSend = (Button) findViewById(R.id.sendBtn);
edt = (EditText) findViewById(R.id.edt);
btnstartconnection= (Button) findViewById(R.id.btnstartConnection);
informations = (TextView) findViewById(R.id.information);
btnEnableDisable_Discoverable = (Button) findViewById(R.id.btnDiscoverable_on_off);
lvNewDevices = (ListView) findViewById(R.id.lvNewDevices);
mBTDevices = new ArrayList<>();
//Broadcasts when bond state changes (ie:pairing)
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
registerReceiver(mBroadcastReceiver4, filter);
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
lvNewDevices.setOnItemClickListener(MainActivity.this);
btnONOFF.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
enableDisableBT();
}
});
btnstartconnection.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startConnection ();
}
});
btnSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
byte [] bytes = edt.getText().toString().getBytes(Charset.defaultCharset());
myBluetoothConnection.write(bytes);
}
});
}
public void startConnection (){
StartBluetoothConnetion(myBluetoothDevice,myUUID);
}
public void StartBluetoothConnetion(BluetoothDevice device , UUID myUUID){
myBluetoothConnection.startClient(device,myUUID);
}
public void enableDisableBT(){
if(mBluetoothAdapter == null){
}
if(!mBluetoothAdapter.isEnabled()){
Toast.makeText(getApplicationContext(), " Enabling Bluetooth ", Toast.LENGTH_SHORT).show();
Intent enableBTIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivity(enableBTIntent);
IntentFilter BTIntent = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
registerReceiver(mBroadcastReceiver1, BTIntent);
}
if(mBluetoothAdapter.isEnabled()){
Toast.makeText(getApplicationContext(), " Disabling Bluetooth ", Toast.LENGTH_SHORT).show();
mBluetoothAdapter.disable();
IntentFilter BTIntent = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
registerReceiver(mBroadcastReceiver1, BTIntent);
}
}
public void btnEnableDisable_Discoverable(View view) {
Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
startActivity(discoverableIntent);
IntentFilter intentFilter = new IntentFilter(mBluetoothAdapter.ACTION_SCAN_MODE_CHANGED);
registerReceiver(mBroadcastReceiver2,intentFilter);
}
public void btnDiscover(View view) {
Toast.makeText(getApplicationContext(), " Searching... ", Toast.LENGTH_SHORT).show();
if(mBluetoothAdapter.isDiscovering()){
mBluetoothAdapter.cancelDiscovery();
//check BT permissions in manifest
mBluetoothAdapter.startDiscovery();
IntentFilter discoverDevicesIntent = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mBroadcastReceiver3, discoverDevicesIntent);
}
if(!mBluetoothAdapter.isDiscovering()){
//check BT permissions in manifest
mBluetoothAdapter.startDiscovery();
IntentFilter discoverDevicesIntent = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mBroadcastReceiver3, discoverDevicesIntent);
}
}
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//first cancel discovery because its very memory intensive.
mBluetoothAdapter.cancelDiscovery();
String deviceName = mBTDevices.get(i).getName();
String deviceAddress = mBTDevices.get(i).getAddress();
informations.setText(deviceName);
informations.setText(deviceAddress);
//create the bond.
//NOTE: Requires API 17+? I think this is JellyBean
if(Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR2){
Toast.makeText(getApplicationContext(), " Paring with " + deviceName, Toast.LENGTH_SHORT).show();
mBTDevices.get(i).createBond();
myBluetoothDevice = mBTDevices.get(i);
myBluetoothConnection = new BluetoothConnection(MainActivity.this);
}
}}
Mainfest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.cy.el.bluetoothtransfer">
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name="com.cy.el.bluetoothtransfer.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
答案 0 :(得分:0)
找不到与给定名称匹配的资源(在'layout_toRightOf'中,值为'@id / edttxt')。 “对那个身份没有控制权
所以使用android:layout_toRightOf="@+id/edttxt"
或定义一个id为edtxt
在有任何疑问时问我
答案 1 :(得分:0)
我在布局中注意到大约3个错误,因为问题来自布局。
第一个是按钮ID为
的按钮机器人:ID =&#34; @ + ID / btnFindUnpairedDevices&#34;,
您将 layout_toRightOf 和 layout_above 设置为相同的值,即 @ ID / edttxt 强>
第二个问题是,当您在关系布局中有子女或兄弟关系时,只有id属性会使用 &#39; +&#39; 符号,但你的布局是文本视图
id =&#34; @ + id / information 有
<强> 机器人:。layout_below =&#34; @ + ID / lvNewDevices&#34; 强>
我认为纠正这些问题应该可以解决问题。