LWUIT J2ME Display.init(his) Error - java

I'm trying to play with LWUIT and siMple app like "hello world"..
But first line in StartApp() - Display.init(this) - causes app to close throwing uncaught exception..
I'm totally confused..
import com.sun.lwuit.Command;
import com.sun.lwuit.Container;
import com.sun.lwuit.Dialog;
import com.sun.lwuit.Display;
import com.sun.lwuit.Form;
import com.sun.lwuit.TextArea;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.layouts.BorderLayout;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class Main extends MIDlet {
private int selectedIndex;
private Form f;
public Main() {}
public void startApp() {
System.out.println("before");
try {
System.out.println("during");
Display.init(this);
} catch(Exception h) {
System.out.println("after");
h.printStackTrace();
}
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO Auto-generated method stub
}
protected void pauseApp() {
// TODO Auto-generated method stub
}
Please help.

Take a look on your imports. I think that the problem is there. I will remove the javax.microedition.lcdui.*;
Here in this web, you can see, how the Nokia UI Demo starts.
Nokia UI DEMO MIDlet
Another solution that I find looking on my code. Put the Display.init(this)in the Midlet constructor instead in the startApp method.

Before any Form is shown the Developer must invoke Display.init(Object m) in order to register the current MIDlet.

Related

Why is getItemInHand being crossed out (see image below)

I am new to creating minecraft plugins, however not new to programming, I am following a tutorial very thoroughly, the video has good ratings so it is trusted, when watching the video the guy has no problems what so ever (Youtube video on developing minecraft plugins) , so I did some research into solutions but always the line through the code.
Eclipse gives me the option for: #SuppressWarnings("deprecation") which allows the code to be used still but I would rather have no need of that usage.
Basically my question is why is there the need of the line going through the code and how do I find a solution to get rid of it.
Main class:
package com.jc1;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.permissions.Permission;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
public class Core extends JavaPlugin
{
public Permission pPermission = new Permission("playerAbilities.allowed");
#Override
public void onEnable()
{
new BlockListener(this);
PluginManager pm = getServer().getPluginManager();
pm.addPermission(pPermission);
}
#Override
public void onDisable()
{
}
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
{
if(cmd.getName().equalsIgnoreCase("giveitems") && sender instanceof Player)
{
Player p = (Player) sender;
if(p.hasPermission("playerAbilities.allowed"))
{
p.setItemInHand(new ItemStack(Material.DIAMOND_BOOTS));
}
return true;
}
return false;
}
}
Secondary class:
package com.jc1;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockPlaceEvent;
public class BlockListener implements Listener
{
public BlockListener(Core plugin)
{
plugin.getServer().getPluginManager().registerEvents(this, plugin);
}
#EventHandler
public void onBlockPlace(BlockPlaceEvent e)
{
Player p = e.getPlayer();
if(!p.hasPermission("playerAbilities.allowed"))
{
e.setCancelled(true);
}
}
}
The method is deprecated, meaning that it is not recommended to be used anymore and is most likely replaced with another method.
Methods that are deprecated may still work as intended.
A simple search for the method reveals (this) documentation, stating:
players can duel wield now use the methods for the specific hand instead
which referes to the #see references:
getItemInMainHand() and getItemInOffHand().
Use this:
player.getInventory().getItemInMainHand()
Instead of:
player.getItemInHand()
Hope this helps! :D

AsyncLoader - wont accept 'this'

This is a follow up to a question I asked here.
I have copied and pasted this code from this tutorial. When I paste it into Android Studio, the 'this' parameter of of content.getLoadManager.initLoader() is highlighted in red and shows the following error:
Wrong 3rd Argument Type. Found 'com.example.carl.loaderDemo.FooLoaderClient', requried: 'android.app.LoaderManager.LoaderCallBacks
I've ran into this previously (see first link). I was hoping this tutorial would help but I just seem to be going in endless circles!
Can anyone point me in the right direction?!
package com.example.carl.loaderdemo;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.AsyncTaskLoader;
import android.support.v4.content.Loader;
public class FooLoader extends AsyncTaskLoader {
public FooLoader(Context context, Bundle args) {
super(context);
// do some initializations here
}
public String loadInBackground() {
String result = "";
// ...
// do long running tasks here
// ...
return result;
}
}
class FooLoaderClient implements LoaderManager.LoaderCallbacks {
Activity context;
// to be used for support library:
// FragmentActivity context2;
public Loader onCreateLoader(int id, Bundle args) {
// init loader depending on id
return new FooLoader(context, args);
}
#Override
public void onLoadFinished(Loader loader, Object data) {
}
public void onLoaderReset(Loader loader) {
// ...
}
public void useLoader() {
Bundle args = new Bundle();
// ...
// fill in args
// ...
Loader loader =
context.getLoaderManager().initLoader(0, args, this);
// with support library:
// Loader loader =
// context2.getSupportLoaderManager().initLoader(0, args, this);
// call forceLoad() to start processing
loader.forceLoad();
}
}
Screenshot of error message:
There is a mismatch in your imports:
import android.support.v4.app.LoaderManager;
import android.support.v4.content.AsyncTaskLoader;
import android.support.v4.content.Loader;
But you need
import android.app.LoaderManager;
import android.content.AsyncTaskLoader;
import android.content.Loader;
You cannot mix the support library with the android framework. Alternatively you can subclass FragmentActivity and call getSupportLoaderManager() instead.
You're implementing android.support.v4.app.LoaderManager.LoaderCallbacks but the client is expecting android.app.LoaderManager.LoaderCallbacks. You need to be consistent in which loader API you're using.

Such a simple lejos program about EV3UltrasonicSensor also can appear mistake?

import lejos.hardware.lcd.LCD;
import lejos.hardware.port.SensorPort;
import lejos.hardware.sensor.EV3UltrasonicSensor;
import lejos.robotics.SampleProvider;
import lejos.utility.Delay;
public class NewUl {
private EV3UltrasonicSensor ev3UltrasonicSensor;
public NewUl() {
// TODO Auto-generated constructor stub
ev3UltrasonicSensor=new EV3UltrasonicSensor(SensorPort.S4);
}
public void getData() {
SampleProvider sampleProvider=ev3UltrasonicSensor.getDistanceMode();
float[] sample=new float[sampleProvider.sampleSize()];
sampleProvider.fetchSample(sample, 0);
LCD.clear();
LCD.drawString(String.valueOf(sample[0]),0,3);
LCD.refresh();
Delay.msDelay(3000);
LCD.clear();
LCD.refresh();
}
public void close() {
ev3UltrasonicSensor.close();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
NewUl newUl=new NewUl();
newUl.getData();
newUl.close();
}
}
This is a simple lejos program about EV3UltrasonicSensor.
The 13th line of code is:
ev3UltrasonicSensor=new EV3UltrasonicSensor(SensorPort.S4);
The 31th line of code is:
NewUl newUl=new NewUl();
The exception thrown is:
Exception in thread "main" java.lang.IllegalArgumentException: Invalid sensor mode at lejos.hardware.sensor.UARTSensor.<init>(UARTSensor.java:62)
at lejos.hardware.sensor.EV3UltrasonicSensor.<init>(EV3UltrasonicSensor.java:75)
at control.NewUl.<init>(NewUl.java:13) at control.NewUl.main(NewUl.java:31)
Such a simple program also can appear mistake? It make me confused.
Could you give me some help or some tips? Any help is appreciated and if you need to know anything more feel free to ask.
From the Exception messages, it looks like that the creation of the instance of lejos.hardware.sensor.EV3UltrasonicSensor is not correct. It may be that the value SensorPort.S4 you passed to the constructor is not proper or you may have to set values to a few more properties of the instance in question.

How to display an image in a java applet in eclipse?

I'm having trouble displaying an image in an applet in eclipse. I'm wondering if there is any alternate way of doing this. I also want to know if there is a reliable online applet tester that displays an applet I made on the web. I'm following the oracle tutorials but they don't work. Here is my code:
Displaying class:
import java.applet.Applet;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
public class usb extends Applet{
static BufferedImage background;
/**
*
*/
private static final long serialVersionUID = 1L;
#Override
public void init() {
// TODO Auto-generated method stub
try {
URL url = new URL(getCodeBase(), "resources/usb_homescreen.png");
background = ImageIO.read(url);
} catch (IOException e) {
}
setSize(1000,500);
add(new goat());
}
}
Canvas class:
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
public class goat extends Canvas {
/**
*
*/
private static final long serialVersionUID = 1L;
goat() {
setSize(1000,500);
setBackground(Color.white);
}
#Override
public void paint(Graphics g) {
// TODO Auto-generated method stub
super.paint(g);
g.drawImage(usb.background, 0, 0, null);
}
}
Any ideas about what's wrong?
Applets are executed in a special environment. When using them as plain objects, this environment is missing, i.e. the init method will never get called and the method getCodeBase() will not work. If you want to test an Applet there’s the program appletviewer.exe shipped with the JDK. It requires a HTML page containing an <applet> or <object> tag.
But there is also a way to instantiate an Applet within a stand-alone java application which will generate the required context. Given an Applet class “MyApplet” the necessary code looks like:
MyApplet applet=(MyApplet)java.beans.Beans.instantiate(
MyApplet.class.getClassLoader(), MyApplet.class.getName());
System.out.println(applet.getCodeBase());//prove that the context is now there
// now you can use applet like a normal AWT/Swing component

FEST: How to use the NoExitSecurityManager properly?

I starting to use FEST to help me to perform unit test on my Java Swing GUI.
For now, I managed to get through the documentation (mostly deprecated) and help me by looking at the Javadoc and the code.
Right now I am stuck on a problem while using the NoExitSecurityManager. The documentation is quite out dated but we can understand the big lines of it.
I simply try to test if my "Quit" MenuItem is working well in my GUI. So, I need to block the System.exit(0) and map the exit status of the program to a JUnit test.
Here is a simplified code I use to perform the test (the tested class is GraphicalUserInterface).
import org.junit.Test;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.fest.swing.annotation.RunsInEDT;
import org.fest.swing.edt.GuiQuery;
import org.fest.swing.edt.GuiActionRunner;
import org.fest.swing.fixture.FrameFixture;
import org.fest.swing.junit.testcase.FestSwingJUnitTestCase;
import org.fest.swing.security.NoExitSecurityManagerInstaller;
public class GraphicalUserInterfaceTest extends FestSwingJUnitTestCase {
private static FrameFixture gui;
private static NoExitSecurityManagerInstaller noExitSecurityManagerInstaller;
#BeforeClass
public static void setUpBeforeClass() throws Exception {
NoExitSecurityManagerInstaller.installNoExitSecurityManager(new ExpectExitSuccess());
}
#AfterClass
public static void tearDownAfterClass() throws Exception {
noExitSecurityManagerInstaller.uninstall();
}
#Override
protected void onSetUp() {
gui = new FrameFixture(robot(), createNewGUI());
gui.show();
}
#RunsInEDT
private GraphicalUserInterface createNewGUI() {
return GuiActionRunner.execute(new GuiQuery<GraphicalUserInterface>() {
protected GraphicalUserInterface executeInEDT() {
return new GraphicalUserInterface();
}
});
}
#Test
public final void testFileMenuQuitMenuItem() {
gui.menuItemWithPath("File", "Quit").click();
}
}
The ExitCallHook are coded like this (you can guess the other one easily).
import static org.junit.Assert.assertTrue;
import org.fest.swing.security.ExitCallHook;
public final class ExpectExitSuccess implements ExitCallHook {
#Override
public void exitCalled(int status) {
assertTrue(status == 0);
}
}
All the tests are performed well and everything seems to be ok except that I get a java.lang.NullPointerException at the end.
So, I wonder what did I do wrong (or what can I improve to not get this nullpointer exception at the end of the test).
I found the solution in the code. In fact, the proper way to do it is the following:
#Test
public final void testFileMenuQuitMenuItem() {
NoExitSecurityManagerInstaller noExitSecurityManagerInstaller =
NoExitSecurityManagerInstaller.installNoExitSecurityManager(new ExpectExitSuccess());
gui.menuItemWithPath("File", "Quit").click();
noExitSecurityManagerInstaller.uninstall();
}
This way prevent to pollute each test with a NoExitSecurityManager.

Categories