使用文本字段会使应用程序崩溃
Using Text Field makes the app crashes
所以我刚刚开始学习 Android Studio 和 LibGDX,所以我不确定如何使用 Scene2d UI 中的 TextField 或者我是否正在使用它正确。当我尝试在模拟器上 运行 下面的代码崩溃了。
package com.mygdx.game;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.TextField;
public class MyGdxGame extends ApplicationAdapter {
private SpriteBatch batch;
private BitmapFont font;
private int screenHeight, screenWidth;
private Skin skin;
private TextField username, password;
@Override
public void create () {
batch = new SpriteBatch();
font = new BitmapFont();
font.setColor(Color.GRAY);
font.getData().scale(3);
screenHeight = Gdx.graphics.getHeight();
screenWidth = Gdx.graphics.getWidth();
skin = new Skin();
username = new TextField("Username", skin);
password = new TextField("Password", skin);
}
@Override
public void dispose() {
batch.dispose();
font.dispose();
skin.dispose();
}
@Override
public void render () {
Gdx.gl.glClearColor(0, 0, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
font.draw(batch, "Username:", (screenWidth / 2) - (screenWidth / 3),
(screenHeight / 2) + (screenHeight / 12));
font.draw(batch, "Password:", (screenWidth/2)-(screenWidth/3),
((screenHeight/2)+(screenHeight/12))-100);
username.draw(batch, 1);
password.draw(batch, 1);
batch.end();
}
}
是的,你的问题是你没有皮肤。
skin = new Skin();
username = new TextField("Username", skin);
您确实创建了一个新皮肤,但它是空的。 TextField 在创建时正在寻找 TextFieldStyle,但有 none,因此您的应用程序会崩溃。
Google 了解如何创建非空皮肤,这应该可以解决您的问题。
所以我刚刚开始学习 Android Studio 和 LibGDX,所以我不确定如何使用 Scene2d UI 中的 TextField 或者我是否正在使用它正确。当我尝试在模拟器上 运行 下面的代码崩溃了。
package com.mygdx.game;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.TextField;
public class MyGdxGame extends ApplicationAdapter {
private SpriteBatch batch;
private BitmapFont font;
private int screenHeight, screenWidth;
private Skin skin;
private TextField username, password;
@Override
public void create () {
batch = new SpriteBatch();
font = new BitmapFont();
font.setColor(Color.GRAY);
font.getData().scale(3);
screenHeight = Gdx.graphics.getHeight();
screenWidth = Gdx.graphics.getWidth();
skin = new Skin();
username = new TextField("Username", skin);
password = new TextField("Password", skin);
}
@Override
public void dispose() {
batch.dispose();
font.dispose();
skin.dispose();
}
@Override
public void render () {
Gdx.gl.glClearColor(0, 0, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
font.draw(batch, "Username:", (screenWidth / 2) - (screenWidth / 3),
(screenHeight / 2) + (screenHeight / 12));
font.draw(batch, "Password:", (screenWidth/2)-(screenWidth/3),
((screenHeight/2)+(screenHeight/12))-100);
username.draw(batch, 1);
password.draw(batch, 1);
batch.end();
}
}
是的,你的问题是你没有皮肤。
skin = new Skin();
username = new TextField("Username", skin);
您确实创建了一个新皮肤,但它是空的。 TextField 在创建时正在寻找 TextFieldStyle,但有 none,因此您的应用程序会崩溃。
Google 了解如何创建非空皮肤,这应该可以解决您的问题。