【问题标题】:Form is not showing on second invocation表单未在第二次调用时显示
【发布时间】:2019-06-05 12:14:39
【问题描述】:

我为应用程序制作了一个简单的侧边菜单。首次调用菜单时。菜单显示正常。

当我第二次重新调用菜单时,我看到一个空白屏幕。

这就是我构建菜单的方式:

package com.mainsys.zappeion;


import static com.codename1.ui.CN.*;
import com.codename1.ui.Form;
import com.codename1.ui.Dialog;
import com.codename1.ui.plaf.UIManager;
import com.codename1.ui.util.Resources;
import com.codename1.io.Log;
import com.codename1.ui.Toolbar;
import com.codename1.ui.FontImage;

/**
 * This file was generated by <a href="https://www.codenameone.com/">Codename One</a> for the purpose 
 * of building native mobile applications using Java.
 */
public class Zappeion {

    private Form current;
    private Resources theme;

    public void init(Object context) {
        // use two network threads instead of one
        updateNetworkThreadCount(2);

        theme = UIManager.initFirstTheme("/theme");

        // Enable Toolbar on all Forms by default
        Toolbar.setGlobalToolbar(true);

        // Pro only feature
        Log.bindCrashProtection(true);

        addNetworkErrorListener(err -> {
            // prevent the event from propagating
            err.consume();
            if(err.getError() != null) {
                Log.e(err.getError());
            }
            Log.sendLogAsync();
            Dialog.show("Connection Error", "There was a networking error in the connection to " + err.getConnectionRequest().getUrl(), "OK", null);
        });        
    }

    public void start() {

        if(current != null){
            current.show();
            return;
        }

        Toolbar tb =    new Toolbar();

        Statues statues = new Statues();
        statues.setToolbar(tb);
        tb.addMaterialCommandToSideMenu("Αγάλματα", FontImage.MATERIAL_ALBUM, e -> { 
            statues.show();              
        });
        Sights sights = new Sights();
        sights.setToolbar(tb);
        tb.addMaterialCommandToSideMenu("Αξιοθέατα", FontImage.MATERIAL_ALBUM, e -> { 
            sights.show();              
        });
       Map map  = new Map();
       map.setToolbar(tb);
       tb.addMaterialCommandToSideMenu("Χάρτης ", FontImage.MATERIAL_ALBUM, e -> { 
            map.show();              
        });

        MainSys mns = new MainSys();
        mns.setToolbar(tb);
        SplashScreen sc = new SplashScreen(); //(sights, mns);
        sc.setToolbar(tb);
        sc.show();
    }

    public void stop() {
        current = getCurrentForm();
        if(current instanceof Dialog) {
            ((Dialog)current).dispose();
            current = getCurrentForm();
        }
    }

    public void destroy() {
    }

}

在上面的代码上,例如,如果我单击菜单上的 Sights Form,它将显示 ok。如果然后我重新单击此菜单,我将得到一个空白屏幕,没有菜单工具栏,什么也没有。

感谢任何帮助。

【问题讨论】:

    标签: codenameone


    【解决方案1】:

    Toolbar 是一个组件,它只能有一个父级,并且您已将其添加到 3。我猜在控制台中会抛出一个异常来指示这一点。由于Toolbar 的独特性,可能没有引发异常。

    我建议您不要创建工具栏,而是使用form.getToolbar();,尤其是因为您的代码已经声明了Toolbar.setGlobalToolbar(true);

    【讨论】:

    • 因此,由于 setGlobalToolbar,如果我为三个表单之一设置工具栏,我将让它在所有表单上都工作。我不需要将命令添加到我添加的每个表单中。我会看看。如果不是这种情况。是否有正确的方法为我的所有表单设置相同的工具栏?
    • 没有。每个表单都有自己的工具栏。您不能对所有三种表单都使用一个工具栏实例。所有组件(工具栏是一个组件)都有一个父级(getParent())如果您将一个组件添加到多个表单,它将不起作用
    【解决方案2】:

    它真正起作用的是下面的代码。我不知道它为什么会起作用,因为我看到的只是代码的重新排序。我改变的另一个想法是我下载了新闻皮肤(Galaxy S8、ipone 8 plus)并在它们上进行了测试。但在那之后它适用于我以前的皮肤(三星s7,iphone 6)。这很奇怪,我不知道是否有人看到更多东西。

    package com.mainsys.zappeion;
    
    
    import static com.codename1.ui.CN.*;
    import com.codename1.ui.Form;
    import com.codename1.ui.Dialog;
    import com.codename1.ui.plaf.UIManager;
    import com.codename1.ui.util.Resources;
    import com.codename1.io.Log;
    import com.codename1.ui.Toolbar;
    import com.codename1.ui.FontImage;
    
    /**
     * This file was generated by <a href="https://www.codenameone.com/">Codename One</a> for the purpose 
     * of building native mobile applications using Java.
     */
    public class Zappeion {
    
        private Form current;
        private Resources theme;
    
        public void init(Object context) {
            // use two network threads instead of one
            updateNetworkThreadCount(2);
    
            theme = UIManager.initFirstTheme("/theme");
    
            // Enable Toolbar on all Forms by default
            Toolbar.setGlobalToolbar(true);
    
            // Pro only feature
            Log.bindCrashProtection(true);
    
            addNetworkErrorListener(err -> {
                // prevent the event from propagating
                err.consume();
                if(err.getError() != null) {
                    Log.e(err.getError());
                }
                Log.sendLogAsync();
                Dialog.show("Connection Error", "There was a networking error in the connection to " + err.getConnectionRequest().getUrl(), "OK", null);
            });        
        }
    
        public void start() {
    
            if(current != null){
                current.show();
                return;
            }        
    
            Statues statues = new Statues();
            Sights sights = new Sights();
            Map map  = new Map();
            MainSys mns = new MainSys();
            SplashScreen sc = new SplashScreen(sights, mns);
            BrowserForm browser = new BrowserForm();
    
            Toolbar tb = statues.getToolbar();
    
            tb.addMaterialCommandToSideMenu("Αρχική", FontImage.MATERIAL_ALBUM, e -> { 
               sc.setTitle("Αρχική");
               sc.show();              
            });
            tb.addMaterialCommandToSideMenu("Αγάλματα", FontImage.MATERIAL_ALBUM, e -> { 
                 statues.setTitle("Αγάλματα");
                 statues.setUrl("https://www.zappeion.gr/el/statues");
                 statues.show();              
            });
    
            tb.addMaterialCommandToSideMenu("Αξιοθέατα", FontImage.MATERIAL_ALBUM, e -> { 
                sights.show();              
            });
    
    
           tb.addMaterialCommandToSideMenu("Χάρτης ", FontImage.MATERIAL_ALBUM, e -> { 
                map.show();              
            });
    
            statues.setToolbar(tb);
            sights.setToolbar(tb);
            map.setToolbar(tb);
            mns.setToolbar(tb);
            sc.setToolbar(tb);
            sc.show();
        }
    
        public void stop() {
            current = getCurrentForm();
            if(current instanceof Dialog) {
                ((Dialog)current).dispose();
                current = getCurrentForm();
            }
        }
    
        public void destroy() {
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2011-08-17
      • 2012-09-02
      • 1970-01-01
      • 2013-02-27
      • 2021-01-05
      • 2015-07-23
      • 1970-01-01
      • 2011-03-20
      • 1970-01-01
      相关资源
      最近更新 更多