简体   繁体   English

startActivity在Android方法中不起作用

[英]startActivity not working in Android Method

I'm currently writing an android app that involves an Activity that creates a canvas and a thread to modify the canvas, and I'm trying to create a button in the options bar to return to the menu. 我目前正在编写一个涉及到一个Activity的android应用程序,该Activity创建一个画布和一个用于修改画布的线程,并且我试图在选项栏中创建一个按钮以返回菜单。 The menu definitely works (as you must pass through it to start the current activity), and the Activity, Canvas, and Thread work (as once you start them they function correctly as far as I can tell), but in the method for returning to the menu Android will not complete the startActivity method following an intent to return to the menu class. 菜单肯定有效(因为必须通过它才能启动当前活动),Activity,Canvas和Thread起作用(据我所知,它们一旦启动就可以正常工作),但是在返回方法中返回菜单类后,Android将无法完成startActivity方法。 This is after I stop the thread and declare the intent, as upon several iterations of different ideas sometimes log cat throws an error at the startActivity line. 这是在我停止线程并声明意图之后,因为不同想法的几次迭代有时会使log cat在startActivity行上引发错误。 Usually, however, I do not get an error, instead I get a switch to a blank black screen that can not be interfaced with, after the animation stops (which as far as can tell is the thread shutting down). 但是,通常情况下,我没有收到错误,而是在动画停止后(切换到关闭状态),我切换到了无法连接的黑屏。 My code is as follows: 我的代码如下:

        switch (item.getItemId()) {
        case R.id.menureturn:
            super.onDestroy();
        try {
            panel.getThread().setRunning(false);
            panel.getThread().join();
        } catch (InterruptedException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        Intent menu = new Intent(MainActivity.this, MenuSplash.class);
        menu.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        startActivity(menu);

            return true;

Any help you can give me would be fantastic! 您能给我的任何帮助都太棒了!

Is this code running on UI thread, if not try make an intent on UI thread like this: 这段代码是否在UI线程上运行,如果不是,请尝试使UI线程像这样:

runOnUiThread(new Runnable(){
    public void run() {
        // This runs on the UI thread
        Intent menu = new Intent(MainActivity.this, MenuSplash.class);
        menu.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        startActivity(menu);
    }
});

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

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