简体   繁体   English

合并Android Studio活动

[英]Merge Android Studio activities

I am having a problem with merging two activities in Android Studio. 我在合并Android Studio中的两个活动时遇到问题。 I am new to coding and I know this should be relatively simple but I am slowly learning, so bear with me. 我是编码的新手,我知道这应该相对简单,但是我正在慢慢学习,所以请多多包涵。

Basically my starting activity was a sample app that came with the IOIO development board. 基本上,我的开始活动是IOIO开发板随附的示例应用程序。 I have modified this to work for my application. 我已经对此进行了修改以适合我的应用程序。 I also have a MAX31855 thermocouple amplifier that I have found code for and it is working perfect, the only problem is that all of the code is in a separate activity from my sample app activity. 我还有一个我已找到代码的MAX31855热电偶放大器,它工作正常,唯一的问题是所有代码都与示例应用程序活动处于单独的活动中。 So both will not run at the same time on my simple one screen app. 因此,两者不会在我简单的单屏应用程序上同时运行。 So now I am trying to merge the thermocouple amplifier code into the sample app code. 因此,现在我尝试将热电偶放大器代码合并到示例应用程序代码中。 How should I start going about this? 我应该如何开始呢? I have attached the code for both activities below. 我已经在下面的两个活动中附加了代码。

The code for the sample app: 示例应用程序的代码:

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

And the code for the thermocouple amplifier: 以及热电偶放大器的代码:

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

}

}

I hope this makes sense and I have provided enough information. 我希望这是有道理的,并且我已经提供了足够的信息。 There is another separate activity for the MAX31855, but I assume this can be left untouched. MAX31855还有另外一个单独的活动,但我认为可以保持不变。 Again, I am slowly learning how java and android studio works, I just can't seem to figure out how to merge these two activities without having a bunch of errors in the code. 再次,我正在慢慢学习java和android studio的工作方式,我似乎无法弄清楚如何在没有代码错误的情况下合并这两个活动。 Any help is appreciated, thank you! 任何帮助表示赞赏,谢谢!

You need to consider three things. 您需要考虑三件事。

1) main.xml Layout file in res->layout 1)res-> layout中的main.xml布局文件

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

3) Single activity which include all codes. 3)包含所有代码的单个活动。

All components should be initialized from a same activity (in your case MainActivity or IOIOSimpleApp). 所有组件都应从同一活动(在您的情况下为MainActivity或IOIOSimpleApp)初始化。

Also remember to include all components (what you have initialized from activity) to the main.xml layout. 还要记住将所有组件(您已从活动初始化的组件)包括到main.xml布局中。

And try this 尝试一下

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

} }

Hope this is work :) 希望这是工作:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM