Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

[1.15.2] GUI Crashing after a few seconds

Featured Replies

Posted

Hello, I'm trying to make a GUI using Screen.

So far I've got 

public class MyGUI extends Screen {
    public MyGUI() {
        super(null);
    }

    @Override
    public void init(Minecraft p_init_1_, int p_init_2_, int p_init_3_) {
        this.addButton(new Button(this.width/2-100, this.height/2-50,200, 100, "Click me!", (button) -> this.onClose()));
    }

    @Override
    public void render(int p_render_1_, int p_render_2_, float p_render_3_) {
        this.renderBackground();
        super.render(p_render_1_, p_render_2_, p_render_3_);
        this.drawCenteredString(this.font, "Click the button!", this.width/2, this.height/2 + 70, 0xFFFFFF);
    }
}

It opens the Screen when I right-click on my block. Right now it draws the background (i.e the screen darkens) but not the buttons/text, then proceeds to freeze the game after a few seconds / pressing Esc.

 

Thanks in advance!

Edited by lolgeny

  • Author

On my block, I have

@Override
public ActionResultType onBlockActivated(BlockState state, World worldIn, BlockPos pos, PlayerEntity player, Hand handIn, BlockRayTraceResult p_225533_6_) {
    Minecraft.getInstance().displayGuiScreen(new MyGUI());
    return ActionResultType.SUCCESS;
}

The log shows nothing abnormal, doesn't mention GUI/crashing and only has an error that I force close the game.

 

How do you construct it normally? That was really the only method I could find to create one from a string, sorry if I'm overlooking something here

  • Author

Thank you for your response! I've changed some things:

on the GUI, I now use 

super(new StringTextComponent("My GUI"));

and to launch it, I use

@Override
    public ActionResultType onBlockActivated(BlockState state, World worldIn, BlockPos pos, PlayerEntity player, Hand handIn, BlockRayTraceResult p_225533_6_) {
        if (worldIn.isRemote()) {
            DistExecutor.runWhenOn(Dist.CLIENT, () -> () -> Minecraft.getInstance().displayGuiScreen(new MyGUI()));
        }
        return ActionResultType.SUCCESS;
    }

Now it crashes with a NullPointerException, not freezing this time: it just goes to 'Saving World' and crashes on the renderBackground method

java.lang.NullPointerException: Rendering screen
	at net.minecraft.client.gui.screen.Screen.renderBackground(Screen.java:367) ~[forge-1.15.2-31.1.46_mapped_snapshot_20200225-1.15.1-recomp.jar:?] {re:classloading,pl:runtimedistcleaner:A}
	

 

  • Author

Thank you! It now loads the GUI, but there's still a problem...

Have I created the button wrongly?

image.png.bda85a8b7a8d25f49057b311972a6927.png

 

  • Author
public class MyGUI extends Screen {
    public MyGUI() {
        super(new StringTextComponent("My GUI"));
    }

    @Override
    public void init() {
        this.addButton(new Button(this.width/2-100, this.height/2-50,200, 100, "Click me!", (button) -> this.onClose()));
    }

    @Override
    public void render(int p_render_1_, int p_render_2_, float p_render_3_) {
        this.renderBackground();
        super.render(p_render_1_, p_render_2_, p_render_3_);
        this.drawCenteredString(this.font, "Yes, click it!", this.width/2, this.height/2 + 70, 0xFFFFFF);
    }
}

 

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

Important Information

By using this site, you agree to our Terms of Use.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.