i cant add toast in libgdx - interface

im trying to create toast by call it with interface
the interface works fine it just that when i try to make a toast with it, it crashes
in Android Launcher
public class AndroidLauncher extends AndroidApplication implements Interface {
public void tost() {
Toast.makeText(getContext(), "wrong num",
Toast.LENGTH_LONG).show();}}
in gameclass
interface Interface{void tost();}
public class MyGdxGame extends ApplicationAdapter {
final private Interface interface;
public MyGdxGame(Interface interface){this.interface=interface;}
public void render() {interface.tost}

Android toasts must be performed on the Android UI thread. The game loop in LibGDX runs on the OpenGL thread, so you must post your method to the UI thread to safely run it, like this:
public class AndroidLauncher extends AndroidApplication implements Interface {
public void tost() {
runOnUiThread(new Runnable() {
#Override
public void run () {
Toast.makeText(getContext(), "wrong num",
Toast.LENGTH_LONG).show();}
}
});
}
}

Related

How to maintain order of methods of interface in implemented class

Suppose I am having below interface inside binary jar:
public interface OrderDemo
{
public void add();
public void shut();
public void manage();
}
When I am adding this as Jar as part of dependency in my project in eclipse after implementing OrderDemo interface order of method get changed.
public class Demo implements OrderDemo
{
#Override
public void add();
{
}
#Override
public void manage()
{
}
#Override
public void shut()
{
}
}
Tried option given in eclipse preference but won't work.

How to add a hook to the Vertx Launcher

I would like to collect metrics with Vert.x Micrometer Metrics, so I need to set proper options to VertxOptions. I run Vertx with Launcher and there is a hook beforeDeployingVerticle but when I override it it's not called.
I overriden Launcher class and beforeDeployingVerticle method but this method is never executed.
public class LauncherTest {
public static class SimpleVerticle extends AbstractVerticle {
#Override
public void start(Future<Void> startFuture) throws Exception {
System.out.println("verticle started");
}
}
public static class LauncherWithHook extends Launcher {
#Override
public void beforeDeployingVerticle(DeploymentOptions deploymentOptions) {
System.out.println("before deploying");
}
}
public static void main(String[] args) {
new LauncherWithHook().execute("run", SimpleVerticle.class.getName());
}
}
In a result I receive just verticle started, but I expect also to have before deploying there. Should I add this hook somehow different?
change your main method like this:
public static void main(String[] args) {
String[] argz = {"run", "your.namepace.LauncherTest$SimpleVerticle"};
LauncherWithHook launcher = new LauncherWithHook();
launcher.dispatch(argz);
}

Working with WorkerStateEvent without casting?

I am currently dispatching my Business Logic via the Concurrency API JavaFX offers. But there is one part I stumble over which does not feel clean to me.
Basically if you create a Service which may look like this
public class FooCommand extends Service<Foo> {
#Override protected Task<Foo> createTask() {
return new Foo();
}
}
and I set the onSucceeded
FooCommand fooCommand = CommandProvider.get(FooCommand.class);
fooCommand.setOnSucceeded(new FooSucceededHandler());
fooCommand.start();
to an instance of this class
public class FooSucceededHandler implements EventHandler<WorkerStateEvent> {
#Override public void handle(WorkerStateEvent event) {
Foo f = (Foo) event.getSource().getValue();
}
}
But as you can see I need to cast the value of the Worker to (Foo). Is there some cleaner way to do it?
You could just make your own abstract class:
public abstract class EventCallback<T> implements EventHandler<WorkerStateEvent> {
#Override
public void handle(final WorkerStateEvent workerStateEvent) {
T returnType = (T) workerStateEvent.getSource().valueProperty().get();
this.handle(returnType);
}
public abstract void handle(T objectReturned);
}
And then using it:
final EventCallback<MonType> eventCallback = new EventCallback<MonType>() {
#Override
public void handle(final MonType objectReturned) {
// DO STUFF
}
};
As it is also an EventHandler, it is compatible with JavaFX concurrent API.

How to list all classes in autocompletion inside annotations in Intellij IDEA?

I have custom class InternalTimerServiceController in my application. I want to use it in another class inside android annotations. And it seems that autocompletion does not work correctly in this case.
I have this interface
public interface InternalTimerServiceControllerContract
{
void doWork();
}
And this class
#EBean
public class InternalTimerServiceController implements InternalTimerServiceControllerContract
{
#Override
public void doWork()
{
// do work
}
}
And this is my Activity
public class MyActivity extends Activity
{
// try uncomment line below and see if autocomplete works properly
//#Bean(Internal)
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
It's a bug, thanks for reporting. I've created a ticket for it: http://youtrack.jetbrains.com/issue/IDEA-98298

Global application class with stack of activities

Stemming from this article more efficient way of updating UI from service I was wondering if I could take that a step further and implement the following. I may have a misunderstanding of my Apps lifecycle though.
public class MyApplication extends Application {
private static final String TAG = MyApplication.class.getSimpleName();
private static Stack<MyActivity> mActivityStack = new Stack<MyActivity>();
private static String mTopActivity = "none";
public static void pushActivity(MyActivity activity)
{
mActivityStack.push(activity);
mTopActivity = activity.getClass().getSimpleName();
Log.i(TAG, "push::"+mTopActivity);
}
public static void popActivity()
{
Log.i(TAG, "pop::"+mTopActivity);
mActivityStack.pop();
}
#Override
public void onLowMemory() {
super.onLowMemory();
Log.w(TAG, "low memory!!!");
Log.w(TAG, "Current::"+mTopActivity);
}
}
public class MyActivity extends Activity
{
private static final String TAG = MyActivity.class.getSimpleName();
public void onCreate(Bundle last)
{
super.onCreate(last);
MyApplication.pushActivity(this);
}
public void onDestroy()
{
super.onDestroy();
MyApplication.popActivity();
}
}
Would the stack be valid during the lifecycle of the application?
As CommonsWare said, this did not work out. Also, it is not a great idea to derive from Activity, because you would then have to also derive listactivity, preferenceactivity, etc. Obviously, I did not think this would solve any problem it was just an experiment in android life cycles.
Would the stack be valid during the lifecycle of the application?
Of course it won't be valid. You assume every activity is created and destroyed in the same sequence. They won't be in many cases (e.g., user presses HOME).
Whatever problem you think you are solving this way, this is not the right solution by any stretch of the imagination.