我没有Java开发和从Eclipse迁移的经验。在需要扩展AppCompactActivityIOIOActivity的情况下,我不知道如何使用嵌套类。考虑到,我有另一个内部类Looper已经扩展了另一个类。下面的代码没有运行Testing类中的内容。有人可以帮我有关如何执行我的内部类(即测试类)的问题。

我的代码:

public class MainActivity extends AppCompatActivity  {

    private class Testing extends IOIOActivity {

        private ToggleButton button_;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            button_ = (ToggleButton) findViewById(R.id.toggleButton);

        }

        class Looper extends BaseIOIOLooper {
            /** The on-board LED. */
            private DigitalOutput led_;

            @Override
            protected void setup() throws ConnectionLostException {
                showVersions(ioio_, "IOIO connected!");
                led_ = ioio_.openDigitalOutput(0, true);
                enableUi(true);
            }
 @Override
            public void loop() throws ConnectionLostException, InterruptedException {
                led_.write(!button_.isChecked());
                Thread.sleep(100);
            }

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

            @Override
            public void incompatible() {
                showVersions(ioio_, "Incompatible firmware version!");
            }
        }

        @Override
        protected IOIOLooper createIOIOLooper() {
            return new Looper();
        }

        private void showVersions(IOIO ioio, String title) {
            toast(String.format("%s\n" +
                            "IOIOLib: %s\n" +
                            "Application firmware: %s\n" +
                            "Bootloader firmware: %s\n" +
                            "Hardware: %s",
                    title,
                    ioio.getImplVersion(IOIO.VersionType.IOIOLIB_VER),
                    ioio.getImplVersion(IOIO.VersionType.APP_FIRMWARE_VER),
                    ioio.getImplVersion(IOIO.VersionType.BOOTLOADER_VER),
                    ioio.getImplVersion(IOIO.VersionType.HARDWARE_VER)));
        }

        private void toast(final String message) {
            final Context context = this;
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(context, message, Toast.LENGTH_LONG).show();
                }
            });
        }

        private int numConnected_ = 0;

        private void enableUi(final boolean enable) {
            // This is slightly trickier than expected to support a multi-IOIO use-case.
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if (enable) {
                        if (numConnected_++ == 0) {
                            button_.setEnabled(true);
                        }
                    } else {
                        if (--numConnected_ == 0) {
                            button_.setEnabled(false);
                        }
                    }
                }
            });
        }
    }
}


谢谢你

最佳答案

我找到了答案,并希望与大家分享。这是用于在Android Studio中启动新的IOIOActivity。 IOIO开发人员尚未编写AppCompactActivity的官方IOIO代码。经过几天的尝试,其最终经过测试并与IOIO合作。


在包中创建一个名为AppCompactIOIOActivity的新Class文件(我喜欢该名称)。注意:所有功劳均归Ytai。 App507中的IOIO代码




public class AppCompactIOIOActivity extends AppCompatActivity implements IOIOLooperProvider {
private final IOIOAndroidApplicationHelper helper_ = new IOIOAndroidApplicationHelper(this, this);

public AppCompactIOIOActivity() {
}

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.helper_.create();
}

protected void onDestroy() {
    this.helper_.destroy();
    super.onDestroy();
}

protected void onStart() {
    super.onStart();
    this.helper_.start();
}

protected void onStop() {
    this.helper_.stop();
    super.onStop();
}

@SuppressLint("WrongConstant")
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    if((intent.getFlags() & 268435456) != 0) {
        this.helper_.restart();
    }

}

protected IOIOLooper createIOIOLooper() {
    throw new RuntimeException("Client must override one of the createIOIOLooper overloads!");
}

public IOIOLooper createIOIOLooper(String connectionType, Object extra) {
    return this.createIOIOLooper();
}


}


然后在您的MainActivity中




public class MainActivity extends AppCompactIOIOActivity {

    private ToggleButton button_;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button_ = (ToggleButton) findViewById(R.id.toggleButton);

    }
    class Looper extends BaseIOIOLooper {
        /** The on-board LED. */
        private DigitalOutput led_;

        @Override
        protected void setup() throws ConnectionLostException {
            showVersions(ioio_, "IOIO connected!");
            led_ = ioio_.openDigitalOutput(0, true);
            enableUi(true);
        }

        @Override
        public void loop() throws ConnectionLostException, InterruptedException {
            led_.write(!button_.isChecked());
            Thread.sleep(100);
        }

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

        @Override
        public void incompatible() {
            showVersions(ioio_, "Incompatible firmware version!");
        }
    }

    @Override
    protected IOIOLooper createIOIOLooper() {
        return new Looper();
    }

    private void showVersions(IOIO ioio, String title) {
        toast(String.format("%s\n" +
                        "IOIOLib: %s\n" +
                        "Application firmware: %s\n" +
                        "Bootloader firmware: %s\n" +
                        "Hardware: %s",
                title,
                ioio.getImplVersion(IOIO.VersionType.IOIOLIB_VER),
                ioio.getImplVersion(IOIO.VersionType.APP_FIRMWARE_VER),
                ioio.getImplVersion(IOIO.VersionType.BOOTLOADER_VER),
                ioio.getImplVersion(IOIO.VersionType.HARDWARE_VER)));
    }

    private void toast(final String message) {
        final Context context = this;
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(context, message, Toast.LENGTH_LONG).show();
            }
        });
    }

    private int numConnected_ = 0;

    private void enableUi(final boolean enable) {
        // This is slightly trickier than expected to support a multi-IOIO use-case.
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (enable) {
                    if (numConnected_++ == 0) {
                        button_.setEnabled(true);
                    }
                } else {
                    if (--numConnected_ == 0) {
                        button_.setEnabled(false);
                    }
                }
            }
        });
    }
}


不要忘记添加您的资源和IOIO开发人员的依赖。祝好运!

10-08 16:50