我在一个文件中包含了此Java代码,该文件是两个文件的混合搭配。原始的现有代码+用于绘制随机数据的代码。 (感谢Dan和keshav帮助使该工作正常进行)。

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

现在将双打代码写入文件:

@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

我想知道是否可以使用意图将双精度值及其具有的值沿代码传递到我想再次使用的地方,但是我知道意图通常用于在 Activity 之间(而不是内部)传递数据。再说一次,我是Java新手,不知道是否有比我想的更简单的方法来做到这一点。如果有人知道一种方法,请帮助。

提前致谢

编辑:我在顶部添加了定义代码的代码:
double doubx etc...

而且有效!那么,对代码进行一些更改,我也对其进行了编辑。感谢Ivan和Sush的回答:D

最佳答案

使doubx,douby,doubz成为外部类的成员变量
像这样:

class OuterClass {

     double doubx;
     double douby;
     double doubz;

     void method {
     String accval = text;
         try {
            PrintWriter writer = new PrintWriter(new BufferedWriter(
                new FileWriter("sdcard/YS Data/Accelerometer.html",
                        true)));

            .....

            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();
        }
     }

     protected class Update extends AsyncTask<Context, Integer, String> {

         .....

     }
}

09-04 13:09