gpt4 book ai didi

java - 同一 Activity 中的 Intent - Android

转载 作者:太空狗 更新时间:2023-10-29 12:44:26 27 4
gpt4 key购买 nike

我将这段 Java 代码放在一个混合和匹配两个文件的文件中。原始现有代码+用于绘制随机数据的代码。 (感谢 Dan 和 keshav 帮助完成这项工作)。

图表显示在绘制随机数据的应用程序上。我想将随机数据更改为我的数据。我有外部加速度计值,在应用程序中进一步加倍,并希望将这些值用于图形。

现在 double 所在的代码,正在写入文件:

@Override
public void onDataRecieved(TiSensor<?> sensor, String text) {
if (sensor instanceof TiAccelerometerSensor) {

final TiAccelerometerSensor accSensor = (TiAccelerometerSensor) sensor;
float[] values = accSensor.getData();
renderer.setRotation(values);

viewText.setText(text);

/**
* The code here, when button clicked, adds the contents of the
* edittext to the same file as x, y, and z
*/
Button confirmButton = (Button) findViewById(R.id.baddNametoFile);
confirmButton.setOnClickListener(new View.OnClickListener() {

public void onClick(View view) {
try {
EditText bodyText;
bodyText = (EditText) findViewById(R.id.ETName);
// bodyText.setInputType(InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS);
// CAPS LOCK
String name = bodyText.getText().toString();
PrintWriter out = new PrintWriter(new BufferedWriter(
new FileWriter(
"/sdcard/YS Data/Accelerometer.html",
true)));
out.println("<hr><br><br><br><h2 style=padding-left:20px;>"
+ name + "'s Data below" + "</h2><br>");
out.flush();
// new arraylist
out.close();
} catch (IOException e) {
Log.e(TAG, "Could not write file " + e.getMessage());
e.printStackTrace();

}

}

});

/**
* Code Below is for splitting the string (text) into three strings
* (x,y,z) of just numbers and then printing them to a file
*/

double doubx;
double douby;
double doubz;
String accval = text;
try {
PrintWriter writer = new PrintWriter(new BufferedWriter(
new FileWriter("sdcard/YS Data/Accelerometer.html",
true)));
String[] tempArr = accval.split("\\s+");
String x1 = tempArr[0];
String y1 = tempArr[1];
String z1 = tempArr[2];

String[] valxsplit = x1.split("=");
String x = valxsplit[1];
String[] valysplit = y1.split("=");
String y = valysplit[1];
String[] valzsplit = z1.split("=");
String z = valzsplit[1];

doubx = Double.parseDouble(x);
douby = Double.parseDouble(y);
doubz = Double.parseDouble(z);

writer.println("<h3 style=padding-left:20px;>" + "x is "
+ doubx + "<br>" + "y is " + douby + "<br>"
+ "Force is " + doubz + "</h3><br>");

writer.close();
} catch (IOException e) {
/** notification possibly */
e.printStackTrace();
}

}

}

//Then lots of code to do other things... then this in graph code

private int generateRandomNum() {
Random randomGenerator = new Random();
int randomInt = randomGenerator.nextInt(10);
return randomInt;

}

protected class Update extends AsyncTask<Context, Integer, String> {
@Override
protected String doInBackground(Context... params) {

int i = 0;
while (true) {

try {
Thread.sleep(700);
x11 = x11 + 5;
y11 = generateRandomNum();
publishProgress(i);
i++;

} catch (Exception e) {

}
}
// return "COMPLETE!";
}

// -- gets called just before thread begins
@Override
protected void onPreExecute() {
super.onPreExecute();

}// then more code... unrelated

我要改y11 , 从上面的代码到 doubx ,来自顶部的代码。当我尝试更改 y11 时至 doub x在上面的代码中,我收到一条错误消息 doubx cannot be resolved to a variable .

我想知道是否可以使用 intents 将 double 以及它们具有的值沿代码传递到我想再次使用它的地方,但是我知道 intents 通常用于传递数据 Activity ,而不是内部。再一次,我是 Java 的新手,不知道是否有比我想象的更简单的方法来做到这一点。如果有人知道方法请帮忙。

提前致谢

编辑:我在顶部添加了定义它们的代码:

double doubx etc...

成功了!好吧,我对代码进行了一些更改,我也对其进行了编辑。感谢 Ivan 和 sush 的回答 :D

最佳答案

在 trycatch 之外定义所有变量,我的意思是将它们作为成员变量。增加范围。都是本地范围内的变量。

        double doubx = Double.parseDouble(x);
double douby = Double.parseDouble(y);
double doubz = Double.parseDouble(z);

关于java - 同一 Activity 中的 Intent - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20786105/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com