我正在使用蓝牙开发一个 Android 应用程序。我想让写入和读取在两个单独的线程中运行。
写线程:
class Write extends Thread {
public void run(){
while(Bluetooth.threadStateWrite){
if(LLTestAppActivity.DEBUG){
Log.d("BLUETOOTH_WRITE", "Write Thread Running!");
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
阅读主题
class Read extends Thread {
public void run(){
while(Bluetooth.threadStateRead){
if(LLTestAppActivity.DEBUG){
Log.d("BLUETOOTH_READ", "Read Thread Running!");
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
我从下面的蓝牙类中调用这两个线程:
public class Bluetooth {
Write write;
Read read;
//Constructor
public Bluetooth() {
write.start();
read.start();
}
写入和读取类在蓝牙类中。
因此,当我尝试实例化蓝牙类时,我在构造函数中得到 NullPointer 异常。谁能指导我如何做到这一点?提前致谢。