我是Android Studio和Java的新手,我尝试使用CO = new Double(sensor0).doubleValue();
将变量sensor0转换为double,并且我想将变量传递给另一个类以使用它。但是我不能在另一个类中使用sensor0的变量。变量的值始终为0。
public class bt extends Activity {
public String sensor0, sensor1;
public double CO;
Button btnOn, btnOff, btnNext;
TextView txtArduino, txtString, txtStringLength, sensorView0, sensorView1, sensorView2, sensorView3;
Handler bluetoothIn;
final int handlerState = 0; //used to identify handler message
private BluetoothAdapter btAdapter = null;
private BluetoothSocket btSocket = null;
private StringBuilder recDataString = new StringBuilder();
private ConnectedThread mConnectedThread;
// SPP UUID service - this should work for most devices
private static final UUID ID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
// String for MAC address
private static String address;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bt);
//Link the buttons and textViews to respective views
btnOn = (Button) findViewById(R.id.buttonOn);
btnOff = (Button) findViewById(R.id.buttonOff);
btnNext = (Button) findViewById(R.id.buttonNext);
txtString = (TextView) findViewById(R.id.txtString);
txtStringLength = (TextView) findViewById(R.id.testView1);
sensorView0 = (TextView) findViewById(R.id.sensorView0);
sensorView1 = (TextView) findViewById(R.id.sensorView1);
sensorView2 = (TextView) findViewById(R.id.sensorView2);
sensorView3 = (TextView) findViewById(R.id.sensorView3);
bluetoothIn = new Handler() {
public void handleMessage(android.os.Message msg) {
if (msg.what == handlerState) { //if message is what we want
String readMessage = (String) msg.obj; // msg.arg1 = bytes from connect thread
recDataString.append(readMessage); //keep appending to string until ~
int endOfLineIndex = recDataString.indexOf("~"); // determine the end-of-line
if (endOfLineIndex > 0) { // make sure there data before ~
String dataInPrint = recDataString.substring(0, endOfLineIndex); // extract string
txtString.setText("Data Received = " + dataInPrint);
int dataLength = dataInPrint.length(); //get length of data received
txtStringLength.setText("String Length = " + String.valueOf(dataLength));
if (recDataString.charAt(0) == '#') //if it starts with # we know it is what we are looking for
{
sensor0 = recDataString.substring(1, 4); //get sensor value from string between indices 1-5
sensor1 = recDataString.substring(5, 8); //same again...
//String sensor2 = recDataString.substring(11, 15);
//String sensor3 = recDataString.substring(16, 20);
sensorView0.setText(" CO2 Value = " + sensor0 + ""); //update the textviews with sensor values
sensorView1.setText(" CO Value = " + sensor1 + "");
// sensorView2.setText(" Sensor 2 Voltage = " + sensor2 + "V");
// sensorView3.setText(" Sensor 3 Voltage = " + sensor3 + "V");
}
recDataString.delete(0, recDataString.length()); //clear all string data
// strIncom =" ";
dataInPrint = " ";
CO = new Double(sensor0).doubleValue();
}
}
}
};
答案 0 :(得分:0)
您需要允许访问此变量。 最有效的方法是为此变量定义一个吸气剂。
答案 1 :(得分:0)
我将CO = new Double(sensor0).doubleValue();
更改为double CO = Double.parseDouble(sensor0);
。然后我通过使用代码从另一个类中获得0
bt SensorCO = new bt();
double COValue = SensorCO.CO;
,
下面是另一个类的代码
public void setBitmap( Bitmap bitmap ) {
oldTime = (System.currentTimeMillis()+500)/1000;
mBitmap = bitmap;
if (!detector.isOperational()) {
//Handle contingency
} else {
//Log.d("time1", SystemClock.currentThreadTimeMillis()+"");
Frame frame = new Frame.Builder().setBitmap(bitmap).build();
mFaces = detector.detect(frame);
}
bt SensorCO = new bt();
double COValue = SensorCO.CO;
newTime = (System.currentTimeMillis()+500)/1000;
timeDifference = (newTime - oldTime);
accumulate = (accumulate + timeDifference);
CameraActivity.showScore(blinkCount, accumulate, COValue);
if(isEyeBlinked()){
accumulate = 0;
Log.d("isEyeBlinked","eye blink is observed");
blinkCount++;
}
if(accumulate > 6){
playSound playSound1 = new playSound();
playSound1.play(accumulate);
}
invalidate();
}
答案 2 :(得分:-2)
确保sensor0具有正确的值,然后 替换
CO = new Double(sensor0).doubleValue();
使用
double CO = Double.parseDouble(sensor0);