0

我正在使用蓝牙开发一个 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 异常。谁能指导我如何做到这一点?提前致谢。

4

1 回答 1

2

you will need to initialize write and read using class Constructor before calling start method as:

public Bluetooth() {    
    write=new Write();  //create Write class Object
    write.start();
    read=new Read();    //create Read class Object
    read.start();

}
于 2013-11-14T06:50:55.823 回答