我希望在libgdx中使用特定于Android的代码。我按照链接中的教程:http://carlorodriguez.github.io/blog/2014/10/05/android-platform-specific-code-with-libgdx/
我的主要课程是:
public class mainclass extends Game implements ApplicationListener {
public ActionResolver mactionResolver;
public mainclass(ActionResolver mactionResolver){
this.mactionResolver = mactionResolver;
};
@Override
public void create() {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
this.mactionResolver.showToast("Hi"); <----here nullpointerexception
}
}
我的android actionresolver是:
public class actionresolverapp implements ActionResolver {
Handler handler;
Context context;
public actionresolverapp(Context context) {
handler = new Handler();
this.context = context;
}
@Override
public void showToast(final CharSequence text) {
handler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
}
});
}
}
最后我在libgdx核心的界面是:
public interface ActionResolver {
public void showToast(CharSequence text);
}
请问为什么我在mainclass中有例外?
编辑:这里是AndroidLauncher
public class AndroidLauncher extends AndroidApplication{
actionresolverapp mactionresolverapp;
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
mactionresolverapp = new actionresolverapp(this);
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
initialize(new mainclass(mactionresolverapp), config);
}
}
答案 0 :(得分:0)
我认为界面一切正常。问题可能出在您在showToast()
中创建的Handler中尝试:
public actionresolverapp (Activity activity, AndroidApplication application){
this.activity = activity;
this.application = application;
}
void showToast(final String text){
Runnable runnable = new Runnable() {
@Override
public void run() {
application.handler.post(new Runnable() { // This thread runs in the UI
@Override
public void run() {
Toast.makeText(activity, text, Toast.LENGTH_SHORT).show();
}
});
}
};
new Thread(runnable).start();
}