我想使用蓝牙为 Android 制作一个简单的应用程序。所以我在网上看了一些例子,我找到了一个。但是我的MainActivity.java
. 只有一个错误,但在“控制台”中没有注意到;它只是用红色下划线。由于这个问题,我无法运行该项目。
这是我的主要内容:
package com.testbluetooth;
import android.os.Bundle;
import com.testbluetooth.R;
import android.app.Activity;
import android.view.Menu;
import java.util.Set;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.widget.TextView;
public class MainActivity extends Activity
{
TextView out;
// Called when the activity is first created.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
//------here the error underlined by Eclipse: -------
out = (TextView) findViewById(R.id.out);
// Getting the Bluetooth adapter
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
out.append("\nAdapter: " + adapter);
// Check for Bluetooth support in the first place
// Emulator doesn't support Bluetooth and will return null
if(adapter==null)
{
out.append("\nBluetooth NOT supported. Aborting.");
//return;
}
// Starting the device discovery
out.append("\nStarting discovery...");
adapter.startDiscovery();
out.append("\nDone with discovery...");
// Listing paired devices
out.append("\nDevices Pared:");
Set<BluetoothDevice> devices = adapter.getBondedDevices();
for (BluetoothDevice device : devices)
{
out.append("\nFound device: " + device);
}
}
}
这是布局中的 *activity_main.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"
tools:context=".MainActivity" >
<TextView
android:id="@+id/out"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/app_name" />
</RelativeLayout>
R.java中的 id 类:
public static final class id {
public static final int menu_settings=0x7f070001;
public static final int out=0x7f070000;
}
所以这out = (TextView) findViewById(R.id.out);
是我有问题的线路。我已经清理了项目,我尝试在布局中创建一个新的 xml 文件,更改了名称,但没有。