合并Android Studio活动

时间:2018-04-04 02:53:09

标签: java android ioio

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

基本上我的开始活动是IOIO开发板附带的示例应用程序。我修改了它以适用于我的应用程序。我还有一个MAX31855热电偶放大器,我找到了代码并且它工作正常,唯一的问题是所有代码都与我的示例应用程序活动分开。所以两者都不会在我的简单的一个屏幕应用程序上同时运行。所以现在我试图将热电偶放大器代码合并到示例应用程序代码中。我该怎么开始这个呢?我已经为下面的两个活动附上了代码。

示例应用的代码:

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还有另外一项活动,但我认为这可以不受影响。再一次,我正在慢慢地学习java和android studio如何工作,我似乎无法弄清楚如何在代码中没有一堆错误的情况下合并这两个活动。感谢任何帮助,谢谢!

1 个答案:

答案 0 :(得分:1)

你需要考虑三件事。

1)res-&gt; layout

中的main.xml布局文件

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

3)单项活动,包括所有代码。

所有组件都应该从同一个活动初始化(在您的案例中为MainActivity或IOIOSimpleApp)。

还要记住将所有组件(从活动初始化的组件)包含到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");
    }

} }

希望这是有效的:)