合并 Android 个工作室活动

Merge Android Studio activities

我在 Android Studio 中合并两个活动时遇到问题。我是编码新手,我知道这应该相对简单,但我正在慢慢学习,所以请耐心等待。

基本上我的开始 activity 是 IOIO 开发板附带的示例应用程序。我修改了它以适用于我的应用程序。我还有一个 MAX31855 热电偶放大器,我找到了它的代码,它工作得很好,唯一的问题是所有代码都在我的示例应用 activity 之外的单独 activity 中。因此,在我的简单单屏应用程序中,两者不会同时 运行。所以现在我正在尝试将热电偶放大器代码合并到示例应用程序代码中。我应该如何开始呢?我在下面附上了这两项活动的代码。

示例应用的代码:

package ioio.examples.simple;

import ioio.lib.api.AnalogInput;
import ioio.lib.api.DigitalOutput;
import ioio.lib.api.IOIO;
import ioio.lib.api.exception.ConnectionLostException;
import ioio.lib.util.BaseIOIOLooper;
import ioio.lib.util.IOIOLooper;
import ioio.lib.util.android.IOIOActivity;
import ioio.lib.api.SpiMaster;
import ioio.lib.api.SpiMaster.Rate;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.ToggleButton;
import java.util.ArrayList;
import java.util.List;

public class IOIOSimpleApp extends IOIOActivity {

private TextView boost;
private TextView fuelpressure;
private TextView ioioStatusText;
private TextView internalText;
private TextView thermocoupleText;
private TextView faultsText;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    boost = (TextView) findViewById(R.id.boost);
    fuelpressure = (TextView) findViewById(R.id.fuelpressure);
    ioioStatusText   = (TextView) findViewById(R.id.ioio_status);
    internalText     = (TextView) findViewById(R.id.internal);
    thermocoupleText = (TextView) findViewById(R.id.thermocouple);
    faultsText       = (TextView) findViewById(R.id.faults);
    enableUi(false);
}

class Looper extends BaseIOIOLooper {
    private AnalogInput boost, fuelpressure;

    @Override
    public void setup() throws ConnectionLostException {
        boost = ioio_.openAnalogInput(45);
        fuelpressure = ioio_.openAnalogInput(42);
        enableUi(true);
    }

    @Override
    public void loop() throws ConnectionLostException, InterruptedException {
        setNumber1(38.314 * ((boost.getVoltage() - 0.27)));
        setNumber2(38.314 * ((fuelpressure.getVoltage() - 0.27)));
        Thread.sleep(200);
    }

    @Override
    public void disconnected() {
        enableUi(false);
    }
}

@Override
protected IOIOLooper createIOIOLooper() {
    return new Looper();
}
private void enableUi(final boolean enable) {
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            //seekBar_.setEnabled(enable);
            //toggleButton_.setEnabled(enable);
        }
    });
}

private void setNumber1(double f) {
    final String str = String.format("%.0f", f);
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            boost.setText(str);
        }
    });
}

private void setNumber2(double f) {
    final String str = String.format("%.0f", f);
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            fuelpressure.setText(str);
        }
    });
}
}

以及热电偶放大器的代码:

package ioio.examples.simple;

import ioio.lib.api.SpiMaster;
import ioio.lib.api.SpiMaster.Rate;
import ioio.lib.api.exception.ConnectionLostException;
import ioio.lib.util.BaseIOIOLooper;
import ioio.lib.util.IOIOLooper;
import ioio.lib.util.android.IOIOActivity;
import ioio.lib.api.AnalogInput;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends IOIOActivity {

protected static final float FAULT_DISPLAY_DURATION = 10; // seconds

private TextView ioioStatusText;
private TextView internalText;
private TextView thermocoupleText;
private TextView faultsText;
private TextView boost;
private TextView fuelpressure;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ioioStatusText   = (TextView) findViewById(R.id.ioio_status);
    internalText     = (TextView) findViewById(R.id.internal);
    thermocoupleText = (TextView) findViewById(R.id.thermocouple);
    faultsText       = (TextView) findViewById(R.id.faults);
    boost            = (TextView) findViewById(R.id.boost);
    fuelpressure     = (TextView) findViewById(R.id.fuelpressure);
}

@Override
protected IOIOLooper createIOIOLooper() {
    int sdoPin = 1; // DO
    int sdaPin = 29; // we do not use this pin but the IOIOLib requires we specify it, so we pick an unused pin
    int sclPin = 2; // CLK
    int csPin  = 3; // CS
    Rate rate = SpiMaster.Rate.RATE_31K;
    final MAX31855 max31855 = new MAX31855(sdoPin, sdaPin, sclPin, csPin, rate);
    max31855.setListener(new MAX31855.MAX31855Listener() {
        private long faultTime;

        @Override
        public void onData(float internal, float thermocouple) {
            updateTextView(internalText, "Internal = " + internal + " C");
            updateTextView(thermocoupleText, thermocouple + " C");

            float secondsSinceFault = (System.nanoTime() - faultTime) / 1000000000.0f;
            if (secondsSinceFault > FAULT_DISPLAY_DURATION) {
                updateTextView(faultsText, "Faults = ");
            }
        }

        @Override
        public void onFault(byte f) {
            List<String> faults = new ArrayList<String>();

            if ((f & MAX31855.FAULT_OPEN_CIRCUIT_BIT) == MAX31855.FAULT_OPEN_CIRCUIT_BIT)
                faults.add("Open Circuit");
            if ((f & MAX31855.FAULT_SHORT_TO_GND_BIT) == MAX31855.FAULT_SHORT_TO_GND_BIT)
                faults.add("Short To GND");
            if ((f & MAX31855.FAULT_SHORT_TO_VCC_BIT) == MAX31855.FAULT_SHORT_TO_VCC_BIT)
                faults.add("Short To VCC");

            boolean first = true;
            String text = "Faults = ";
            for (String fault : faults) {
                if (!first)
                    text += ", ";
                text += fault;
            }
            if (faults.size() > 0) {
                faultTime = System.nanoTime();
            }

            updateTextView(faultsText, text);
        }
    });
    return new DeviceLooper(max31855);
}

private void updateTextView(final TextView textView, final String text) {
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            textView.setText(text);
        }
    });
}

/**
 * This is the thread on which all the IOIO activity happens. It will be run
 * every time the application is resumed and aborted when it is paused. The
 * method setup() will be called right after a connection with the IOIO has
 * been established (which might happen several times!). Then, loop() will
 * be called repetitively until the IOIO gets disconnected.
 */
class DeviceLooper extends BaseIOIOLooper {

    private IOIOLooper device;

    public DeviceLooper(IOIOLooper device) {
        this.device = device;
    }

    @Override
    public void setup() throws ConnectionLostException, InterruptedException {
        device.setup(ioio_);
        updateTextView(ioioStatusText, "IOIO Connected");
    }

    @Override
    public void loop() throws ConnectionLostException, InterruptedException {
        device.loop();
    }

    @Override
    public void disconnected() {
        device.disconnected();
        updateTextView(ioioStatusText, "IOIO Disconnected");
    }

    @Override
    public void incompatible() {
        updateTextView(ioioStatusText, "IOIO Incompatible");
    }

}

}

我希望这是有道理的,我已经提供了足够的信息。 MAX31855 有另一个单独的 activity,但我认为可以保持不变。同样,我正在慢慢学习 java 和 android studio 的工作原理,我似乎无法弄清楚如何在代码中没有一堆错误的情况下合并这两个活动。感谢任何帮助,谢谢!

你需要考虑三件事。

1) main.xml 布局文件在 res->layout

2) AndroidManifest.xml 在清单文件夹中

3) 包含所有代码的单个 activity。

所有组件都应该从同一个 activity 初始化(在您的情况下是 MainActivity 或 IOIOSimpleApp)。

还记得将所有组件(您从 activity 初始化的组件)包含到 main.xml 布局中。

然后试试这个

public class IOIOSimpleApp extends IOIOActivity {

protected static final float FAULT_DISPLAY_DURATION = 10; // seconds

private TextView boost;
private TextView fuelpressure;
private TextView ioioStatusText;
private TextView internalText;
private TextView thermocoupleText;
private TextView faultsText;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    boost = (TextView) findViewById(R.id.boost);
    fuelpressure = (TextView) findViewById(R.id.fuelpressure);
    ioioStatusText   = (TextView) findViewById(R.id.ioio_status);
    internalText     = (TextView) findViewById(R.id.internal);
    thermocoupleText = (TextView) findViewById(R.id.thermocouple);
    faultsText       = (TextView) findViewById(R.id.faults);


    //components in main activity
    ioioStatusText   = (TextView) findViewById(R.id.ioio_status);
    internalText     = (TextView) findViewById(R.id.internal);
    thermocoupleText = (TextView) findViewById(R.id.thermocouple);
    faultsText       = (TextView) findViewById(R.id.faults);
    boost            = (TextView) findViewById(R.id.boost);
    fuelpressure     = (TextView) findViewById(R.id.fuelpressure);

    enableUi(false);
}

class Looper extends BaseIOIOLooper {
    private AnalogInput boost, fuelpressure;

    @Override
    public void setup() throws ConnectionLostException {
        boost = ioio_.openAnalogInput(45);
        fuelpressure = ioio_.openAnalogInput(42);
        enableUi(true);
    }

    @Override
    public void loop() throws ConnectionLostException, InterruptedException {
        setNumber1(38.314 * ((boost.getVoltage() - 0.27)));
        setNumber2(38.314 * ((fuelpressure.getVoltage() - 0.27)));
        Thread.sleep(200);
    }

    @Override
    public void disconnected() {
        enableUi(false);
    }
}

@Override
protected IOIOLooper createIOIOLooper() {
    int sdoPin = 1; // DO
    int sdaPin = 29; // we do not use this pin but the IOIOLib requires we specify it, so we pick an unused pin
    int sclPin = 2; // CLK
    int csPin  = 3; // CS
    SpiMaster.Rate rate = SpiMaster.Rate.RATE_31K;
    final MAX31855 max31855 = new MAX31855(sdoPin, sdaPin, sclPin, csPin, rate);
    max31855.setListener(new MAX31855.MAX31855Listener() {
        private long faultTime;

        @Override
        public void onData(float internal, float thermocouple) {
            updateTextView(internalText, "Internal = " + internal + " C");
            updateTextView(thermocoupleText, thermocouple + " C");

            float secondsSinceFault = (System.nanoTime() - faultTime) / 1000000000.0f;
            if (secondsSinceFault > FAULT_DISPLAY_DURATION) {
                updateTextView(faultsText, "Faults = ");
            }
        }

        @Override
        public void onFault(byte f) {
            List<String> faults = new ArrayList<String>();

            if ((f & MAX31855.FAULT_OPEN_CIRCUIT_BIT) == MAX31855.FAULT_OPEN_CIRCUIT_BIT)
                faults.add("Open Circuit");
            if ((f & MAX31855.FAULT_SHORT_TO_GND_BIT) == MAX31855.FAULT_SHORT_TO_GND_BIT)
                faults.add("Short To GND");
            if ((f & MAX31855.FAULT_SHORT_TO_VCC_BIT) == MAX31855.FAULT_SHORT_TO_VCC_BIT)
                faults.add("Short To VCC");

            boolean first = true;
            String text = "Faults = ";
            for (String fault : faults) {
                if (!first)
                    text += ", ";
                text += fault;
            }
            if (faults.size() > 0) {
                faultTime = System.nanoTime();
            }

            updateTextView(faultsText, text);
        }
    });
    return new IOIOSimpleApp.DeviceLooper(max31855);
}
private void enableUi(final boolean enable) {
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            //seekBar_.setEnabled(enable);
            //toggleButton_.setEnabled(enable);
        }
    });
}

private void setNumber1(double f) {
    final String str = String.format("%.0f", f);
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            boost.setText(str);
        }
    });
}

private void setNumber2(double f) {
    final String str = String.format("%.0f", f);
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            fuelpressure.setText(str);
        }
    });
}
private void updateTextView(final TextView textView, final String text) {
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            textView.setText(text);
        }
    });
}


/**
 * This is the thread on which all the IOIO activity happens. It will be run
 * every time the application is resumed and aborted when it is paused. The
 * method setup() will be called right after a connection with the IOIO has
 * been established (which might happen several times!). Then, loop() will
 * be called repetitively until the IOIO gets disconnected.
 */
class DeviceLooper extends BaseIOIOLooper {

    private IOIOLooper device;

    public DeviceLooper(IOIOLooper device) {
        this.device = device;
    }

    @Override
    public void setup() throws ConnectionLostException, InterruptedException {
        device.setup(ioio_);
        updateTextView(ioioStatusText, "IOIO Connected");
    }

    @Override
    public void loop() throws ConnectionLostException, InterruptedException {
        device.loop();
    }

    @Override
    public void disconnected() {
        device.disconnected();
        updateTextView(ioioStatusText, "IOIO Disconnected");
    }

    @Override
    public void incompatible() {
        updateTextView(ioioStatusText, "IOIO Incompatible");
    }

} }

希望这是工作:)