FileSystemStorage issue - codenameone

this issue start appeared in some android devices before two weeks
Display.getInstance().openGallery(new ActionListener<ActionEvent>() {
#Override
public void actionPerformed(ActionEvent evt) {
if(evt!=null&&evt.getSource()!=null){
String imageFilePath = (String) evt.getSource();
Image.createImage(FileSystemStorage.getInstance().openInputStream(imageFilePath));
}
}, Display.GALLERY_IMAGE);
FileSystemStorage.getInstance().openInputStream(imageFilePath)
java.lang.stackoverflowerror: stack size …
the app stuck and after while the error appeared

add build hint
codename1.arg.android.xapplication_attr=android:requestLegacyExternalStorage="true"

Related

Map does not fire Pointer Pressed/Release events

I have a MapContainer instance that does not respond to attached PointerRelease listeners. I'm trying to detect when the map has stopped moving once it's been dragged by the user. In the code below the first two listeners do not produce any output. Third/Fourth do.
MapContainer mc = new MapContainer("mykey");
mc.addPointerPressedListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent evt) {
System.out.println("Don't see this");
}
});
mc.addPointerReleasedListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent evt) {
System.out.println("Don't see this either");
}
});
mc.addTapListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent evt) {
System.out.println("See This :-)");
}
});
mc.addMapListener(new MapListener() {
#Override
public void mapPositionUpdated(Component source, int zoom, Coord centerCoord) {
System.out.println("See this too!");
}
});
addTapListener is correct and implemented in MapContainer. The low level pointer events won't work properly for that container because the map logic is implemented natively and thus these events are consumed.
You might be able to track them by binding a listener to the parent form but that's problematic as events/gestures might have platform specific interpretations.

Null pointer exception for Extent Report

It wouldbe silly question but would be great help it you can help me out.
I tried implementing extent report for multple test cases, but report are not getting generated .
Code:
public class SampleTc1
{
static WebDriver driver;
static ExtentReports report;
static ExtentTest logger;
static void testcase1()
{
System.setProperty("webdriver.chrome.driver","chromedriver.exe");
driver = new ChromeDriver();
driver.get("https://www.google.co.in");
logger.log(LogStatus.PASS, "This step is passed");
driver.close();
}
}
public class SampleTc2
{
static WebDriver driver;
static ExtentReports report;
static ExtentTest logger;
static void testcase2()
{
System.setProperty("webdriver.chrome.driver","chromedriver.exe");
driver = new ChromeDriver();
driver.get("https://www.google.co.in");
logger.log(LogStatus.PASS, "This step is passed");
driver.close();
}
}
Main Class:
public class Maindriver {
static WebDriver driver;
static ExtentReports report;
static ExtentTest logger;
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
report=new ExtentReports("./Report/ExtentReport/ExecutionResult.html", true);
logger=report.startTest("TC1", "Testc Case1");
SampleTc1.testcase1();
report.endTest(logger);
logger=report.startTest("TC2", "Testc Case2");
SampleTc2.testcase2();
report.endTest(logger);
report.flush();
}
}
After running no reports are getting generated and it is showing null ponter exception:
Exception in thread "main" java.lang.NullPointerException
at SmokeTest.SampleTc1.testcase1(SampleTc1.java:24)
at SmokeTest.Maindriver.main(Maindriver.java:22)
Above exception I am getting.
Thanks in advance.
If you are using testng for running selenium suite, you can implement extent reports as a listener.
Like,
public class ExtentReporterNG implements IReporter
{
public ExtentReports extent;
private void buildTestNodes(IResultMap testMap, LogStatus status)
{
ExtentTest test;
if (testMap.size() > 0)
{
for (ITestResult result : testMap.getAllResults())
{
//test = extent.startTest(result.getInstance().getClass().getSimpleName(),result.getMethod().getMethodName());
test = extent.startTest(result.getMethod().getMethodName().toUpperCase(),result.getInstance().getClass().getSimpleName().toUpperCase());
test.assignCategory(result.getInstance().getClass().getSimpleName().toUpperCase());
test.setStartedTime(getTime(result.getStartMillis()));
for (String group : result.getMethod().getGroups())
test.assignCategory(group);
String message = "Test " + status.toString().toLowerCase() + "ed";
if (result.getThrowable() != null)
message = result.getThrowable().getMessage();
test.setEndedTime(getTime(result.getEndMillis()));
test.log(status, message);
extent.endTest(test);
}
}
}
private Date getTime(long millis)
{
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(millis);
return calendar.getTime();
}
#Override
public void generateReport(List<XmlSuite> xmlsuite, List<ISuite> suites,String file)
{
final String filePath=GlobalSettings.getProperty(GlobalSettings.EXTENTFILE);
extent = new ExtentReports(filePath, true,DisplayOrder.NEWEST_FIRST,NetworkMode.OFFLINE );
extent.loadConfig(new File("./config/extentConfig.xml"));
for (ISuite suite : suites)
{
Map<String, ISuiteResult> result = suite.getResults();
for (ISuiteResult r : result.values())
{
ITestContext context = r.getTestContext();
buildTestNodes(context.getPassedTests(), LogStatus.PASS);
buildTestNodes(context.getFailedTests(), LogStatus.FAIL);
buildTestNodes(context.getSkippedTests(), LogStatus.SKIP);
buildTestNodes(context.getFailedConfigurations(),LogStatus.FAIL);
buildTestNodes(context.getSkippedConfigurations(),LogStatus.SKIP);
}
}
extent.flush();
extent.close();
}}
It is not well organized, hard to say what/where is the problem ...
You can check here a selenium webdriver testng tutorial I used it when I started and it is a good starting point!
You have to change your ExtentReports as static in the base class. I got this idea from Mukesh Otwani, Selenium automation trainer for Learn-Automation.
I created separate class for extent report, hence avoided NULL pointer exception

CodenameOne MapContainer Zoom Level

I am using the MapContainer(cn1lib). so in android devices low relsolution the zoom works fine. But in android devices high resolution the zoom not works fine. The zoom in stay far. i attach a screen with the to max zoom in, it is a bug or i'm wrong?
SCREENSHOT
GUI-DESIGN
public class StateMachine extends StateMachineBase {
MapContainer mapContainer;
public StateMachine(String resFile) {
super(resFile);
// do not modify, write code in initVars and initialize class members there,
// the constructor might be invoked too late due to race conditions that might occur
}
/**
* this method should be used to initialize variables instead of the
* constructor/class scope to avoid race conditions
*/
protected void initVars(Resources res) {
}
#Override
protected void beforeShow(Form f) {
try {
this.mapContainer.setShowMyLocation(true);
this.mapContainer.zoom(new Coord(20.640086, -103.432207), 17);
this.mapContainer.setCameraPosition(new Coord(20.640086, -103.432207));
this.mapContainer.addMarker(
EncodedImage.createFromImage(fetchResourceFile().getImage("pin.png"), false),
new Coord(20.640086, -103.432207),
"Hi marker", "Optional long description",
new ActionListener() {
public void actionPerformed(ActionEvent evt) {
Dialog.show("Marker Clicked!", "You clicked the marker", "OK", null);
}
}
);
this.mapContainer.addPointerDraggedListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent evt) {
mapContainer.clearMapLayers();
mapContainer.addMarker(EncodedImage.createFromImage(fetchResourceFile().getImage("pin.png"), false), mapContainer.getCameraPosition(), "Hi marker", "Optional long description", new ActionListener() {
public void actionPerformed(ActionEvent evt) {
Dialog.show("Marker Clicked!", "You clicked the marker", "OK", null);
}
});
}
});
} catch (Exception ex) {
ex.printStackTrace();
}
super.beforeShow(f); //To change body of generated methods, choose Tools | Templates.
}
#Override
protected Component createComponentInstance(String componentType, Class cls) {
if (cls == MapComponent.class) {
this.mapContainer = new MapContainer();
return this.mapContainer;
}
return super.createComponentInstance(componentType, cls); //To change body of generated methods, choose Tools | Templates.
}
}
That is a MapComponent not a native map, so it uses the old open street maps support and relatively simple map rendering even on the device. We have support for native google maps which isn't exposed in the GUI builder but you can add it thru code.
This will embed the actual native GUI into place which will both look and feel better on the device although it will look the same on the simulator.

Unable to display toast messages

I have download and use the code form the following URL
https://github.com/Pmovil/Toast to display toast message.
Initially I got NativeToastImpl Not implemented error. I have resolved by coping the native related code to my project. Now the System throws Runtime Exception "Toast is not supported in this platform."
Here is my code to display toast message.
public class MyApplication {
private Form current;
private static Object context;
public void init(Object context) {
MyApplication.context = context;
}
public static Object getContext() {
return context;
}
public void start() {
if (current != null) {
current.show();
return;
}
showLoginForm();
}
public void stop() {
current = Display.getInstance().getCurrent();
}
public void destroy() {
}
private void showLoginForm() {
Form form = new Form("WelCome ...");
Button b = new Button(" Login ");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
Log.p(" Came hgere ");
Log.p(" *** " + MyApplication.getContext());
Toast.makeText(MyApplication.getContext(), "HI", Toast.LENGTH_LONG);
}
});
form.addComponent(b);
form.show();
}}
I have used Net Beans IDE for development, OS : windows 8.1
Please let me know I am doing wrong in this code and
Is there any other way to display toast messages using codename one?.
Thanks in advance
please edit the following code and please test the toast in device . Toast is not available in emulator.
public void init(Object context) {
this.context = context;
}
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
Log.p(" Came hgere ");
Toast.makeText(context, "HI", Toast.LENGTH_LONG);
}
});
You missed the show() method on Toast.
Toast.makeText(MyApplication.getContext(), "HI", Toast.LENGTH_LONG).show();

catching unknown host exception in codename one

I am building an app using codename one
So the thing is, I need to access a URL using the app. THe URL brings back some result which I show on the screen.
SO I use these lines to do that :
ConnectionRequest c = new ConnectionRequest() {
protected void readResponse(InputStream input) throws IOException {
ByteArrayOutputStream bs = new ByteArrayOutputStream();
int ch;
while ((ch = input.read()) != -1) {
bs.write(ch);
}
serverOutput = new String(bs.toByteArray());
bs.close();
}
};
c.setUrl("My URL HERE");
c.setPost(false);
NetworkManager.getInstance().addToQueueAndWait(c);
So, now , if the gprs is active, this code works fine.
BUT , if the GPRS is inactive, it throws an Unknow Host Exception
SO to catch this error, i TRIED to use a try catch block like this:
try{
NetworkManager.getInstance().addToQueueAndWait(c);
}
catch(Exception e)
{
Sys.out.pln(e.troString());
}
But, i still get the error in the form of a dialog in the app. How do i catch this error and put my own handling for it?
UPDATE 1:
Am not sure this is necessarily a codename one specific questions, or related to java ...so just help me out with this.
Try this to handle generic errors for all connections:
NetworkManager.getInstance().addErrorListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
//handle your error here consume the event
evt.consume();
}
});
Or override:
protected void handleErrorResponseCode(int code, String message) {
}
And:
protected void handleException(Exception err) {
}
In your connection request code to do this for just one class.
Try it...
public void init(Object context) {
Display.getInstance().addEdtErrorHandler(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
evt.consume();
Throwable exception = (Throwable) evt.getSource();
}
});
}

Resources