Form is not showing on second invocation

Viewed 32

I have made a simple side menu for an application. When Invoce the menu for the first time. The menu is showing ok.

When I reinvoke the menu on second time I see a blank screen.

This is how I build the menu:

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() {
    }

}

ON the above code if for example I click on the menu the Sights Form it will show ok. If then I reclick on this menu I wil get a blank screen with with no menu toolbar and nothing.

any help is appreciated.

2 Answers

Toolbar is a component, it can have only one parent and you've added it to 3. I would guess there is an exception thrown along the way in the console to indicate that. It's possible that due to the unique nature of Toolbar the exception wasn't thrown.

I would suggest you don't create a toolbar and instead use form.getToolbar(); especially because your code already declared Toolbar.setGlobalToolbar(true);.

What it actually worked is the code below. I don't know why it is working, because from what I see is just a reordering of code. One more think I changed was that I downloaded news skins (Galaxy S8, ipone 8 plus) and tested on them. But after that it worked on my previous skin (Samsung s7, iphone 6). It is weird, I dont know if someone seeing something more.

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() {
    }

}
Related