是否可以ArrayList
在扩展的类中使用Thread
?我已经编写了一些代码来从正在工作的蓝牙串行读取,但我添加了一个ArrayList
来跟踪我正在阅读的内容。但是,我正在NullPointException
访问任何可以访问results的线路。我知道这ArrayList
不是线程安全的(尽管我可能不完全理解这意味着什么)所以我可能正在尝试做不可能的事情,但我想我会向专家咨询,因为 ArrayLists 否则很容易使用。
private class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private List<String> results;
public ConnectedThread(BluetoothSocket socket) {
Log.d(TAG, "create ConnectedThread");
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
List<String> results = new ArrayList<String>();
// Get the BluetoothSocket input and output streams
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) {
Log.e(TAG, "temp sockets not created", e);
}
mmInStream = tmpIn;
mmOutStream = tmpOut;
init();
}
private void init(){
code = new byte[16];
checksum = 0;
tempbyte = 0;
bytesread = -1;
if(!results.isEmpty()){
Log.i(TAG, "Already have found: " + results.size() + " things.");
}
}
public void run(){
//do things
}
}