Android - put an image name "package" throws compile time error - android

I am trying to put an image name "package.png" in my drawable folder. As soon as i paste them it into the folder it is throwing following error:
[2012-05-26 12:40:30 - MyApp] res/drawable-mdpi/package.png:0: error: invalid symbol: 'package'
But as soon as i rename to some other name, this image works fine. Any idea why this is happening and how i can fix this and have a drawable named "package".

you cant do that, as it is a Reserved word. just like...
break else new var
case finally return void
catch for switch while
continue function this with
default if throw
delete in try
do instanceof typeof
abstract enum int short
boolean export interface static
byte extends long super
char final native synchronized
class float package throws
const goto private transient
debugger implements protected volatile
double import public
null
true
false
Each resource having entry in java field name inside R.java class:
drawable\package.png -> R.drawable.package // while package is a reserved keyword in Java(mentioned above)

Related

Input stream must not be null exception in gradle project

I have MainClass
public class MainClass extends Application {
#Override
public void start(Stage primaryStage) {
try{
Image img = new Image(getClass().getResourceAsStream(".\\build\\resources\\main\\img\\h1.jpg"));
System.out.println("ok");
}
catch(Exception ex){
System.out.println(ex.getMessage());
}
}
public static void main(String[] args) {
launch(args);
}
}
and my image is in \build\resources\main\img\h1.jpg directory
my project files:
When I run project it gives Input stream must not be null exception.
Resource paths are not separated by \ Furthermore they start at the resource root. In this case the path "/img/h1.jpg" should do the trick assuming your IDE properly includes the resources in the classpath at runtime.
getResource(AsStream) does not access the data via file path; The data may not be available as file at all, but as entry in a JAR file. If you need to refer to a file that is not included in the classpath, use File's functionality to convert to a URI or use a FileInputStream:
new Image(new File(".\\build\\resources\\main\\img\\h1.jpg").toURI().toString())

Calling static jar function from Unity3D

I made and compiled a Android Library, containing a simple class and a simple static function:
package moo;
public class MyTestClass {
public static String Foo(){
return "Foo from Moo";
}
}
I placed the .jar in my Assets/Plugins/Android Folder. Then In Unity:
void OnGUI () {
string somestring = "foooooooooooOOooo";
AndroidJavaClass testClass = new AndroidJavaClass("moo.MyTestClass");
somestring = testClass.CallStatic<string>("Foo");
GUI.Label (new Rect (20, 20, 100, 20), somestring);
}
And I get an error:
JNI: Unable to find method id for 'Foo' (static)
UnityEngine.AndroidJavaObject:CallStatic(String, Object[])
Am I missing something to call my static method?
Thanks!
there are 2 problem as far as I can see:
you have to put your jar package to Assets/Plugins/Android/bin;
you will always get this error on your windows/mac editor, you have to run this on your android device;

The name 'Assets' does not exist in the current context

I have a problem with this line of code. Want to create sqlite database on the device.
string dbPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal) + "\\test.db";
if (!System.IO.File.Exists(dbPath))
using (System.IO.Stream sr = ***Assets***.Open("test.db"))
{
using (System.IO.Stream srTo = System.IO.File.Create(dbPath))
{
sr.CopyTo(srTo);
}
}
This message gives the:
The name 'Assets' does not exist in the current context
have a similar project, but more comprehensive than big. There is no error. They Assets Where I'm following definition defines a cs file in c drive gives reference to Android.Content.ContextWrapper.
is not on The project the path.
[How the file was added application?
If you aren't doing this in an activity you need a reference to the Activity/Context. You will need to pass this in to your helper class in the constructor.
public yourClass(Activity context.......){
context.Assets.Open("your.db");
}
You can do like this:
using (System.IO.Stream sr = Android.App.Application.Context.Assets.Open("test.db"))
{
using (System.IO.Stream srTo = System.IO.File.Create(dbPath))
{
sr.CopyTo(srTo);
}
}

Android, generate jni Header Files with javah , show error that can't find org.opencv.core.Mat

I just have an annoying problem with jni when i compile the native method in java class with javah to generate JNI header files.
If the class has used 3rd-party package, For example: org.opencv.core.Mat, then the javah will show the error that can't find the org.opencv.core.Mat class.
The OpenCV sample code as below:
package org.opencv.samples.fd;
import org.opencv.core.Mat;
import org.opencv.core.MatOfRect;
public class DetectionBasedTracker
{
public DetectionBasedTracker(String cascadeName, int minFaceSize) {
mNativeObj = nativeCreateObject(cascadeName, minFaceSize);
}
public void start() {
nativeStart(mNativeObj);
}
public void stop() {
nativeStop(mNativeObj);
}
public void setMinFaceSize(int size) {
nativeSetFaceSize(mNativeObj, size);
}
public void detect(Mat imageGray, MatOfRect faces) {
nativeDetect(mNativeObj, imageGray.getNativeObjAddr(), faces.getNativeObjAddr());
}
public void release() {
nativeDestroyObject(mNativeObj);
mNativeObj = 0;
}
private long mNativeObj = 0;
private static native long nativeCreateObject(String cascadeName, int minFaceSize);
private static native void nativeDestroyObject(long thiz);
private static native void nativeStart(long thiz);
private static native void nativeStop(long thiz);
private static native void nativeSetFaceSize(long thiz, int size);
private static native void nativeDetect(long thiz, long inputImage, long faces);
}
First, I used the command
javah -classpath bin/classes -bootclasspath (the directory of android.jar) -d jni (packageName + ClassName) , shows the error "can't find the org.opencv.core.Mat
Then I modified the command to
javah - classpath bin/classes - bootclasspath (the dir of android.jar) ; (the dir of the opencv lib jar) -d jni ..." ", this time it shows error
Exception
Exception in thread "main" java.lang.IllegalArgumentException: Not a valid class
name: E:\Computer_Language\Java\soft_android\OpenCV-2.4.3-rc-android-sdk\OpenCV
-2.4.3-rc-android-sdk\sdk\java\bin\opencv library - 2.4.3.jar
at com.sun.tools.javac.api.JavacTool.getTask(JavacTool.java:177)
at com.sun.tools.javac.api.JavacTool.getTask(JavacTool.java:68)
at com.sun.tools.javah.JavahTask.run(JavahTask.java:509)
at com.sun.tools.javah.JavahTask.run(JavahTask.java:335)
at com.sun.tools.javah.Main.main(Main.java:46)
I think, adding the directory of opencv lib in -bootclasspath is useful and neccessary. The error is because i just added two path in -bootclasspath or the format is something wrong?
Really confused.
Please give some help , thank u!
This is what I did:
1.Open Command line, type to the (project)/bin/classes:
2.type: javah -classpath (opencv4android sdk path)/java/bin/classes:(your project location)/bin/classes -jni (your java class file that contains the native library interfaces)
In my project. I did:
javah -classpath /home/zijun/Dev/adt/OpencvAndroid/sdk/java/bin/classes:/home/zijun/workspace/LocTM/bin/classes -jni com.brainport.loctm.TMatching
That works on Linux Ubuntu 12.04.02 64bit OS
I encountered the same problem too,it cost me half day.I just copy DetectionBasedTracker.java to my poject.I use Android Studio.When I use ExternalTools>javah to generate .h files,the console print cant find org.opencv.core.Mat.copying mat.java & matofRect.java to the directory DetectionBasedTracker.java exists just cause more java class cant be find.finally,I find this problem was caused by the import * clause in java file.and we seldom use java class in native method we defined.so I cut these java method and got this:
package cn.ntu.tonguefur.nativemethod;
public class DetectionBasedTracker{
private static native long nativeCreateObject(String cascadeName,int minFaceSize);
//Other native methods
private static native void nativeDetect(long thiz,long inputImage,long faces);
}
Now you can freely use javah command to generate .h
file.After that,add java methods and import packages you need.

Jersey on Jetty on Android throws ContainerException: The ResourceConfig instance does not contain any root resource classes

I'm trying to run Jersey on Jetty on Android.
I've created an Android that instantiate a Jetty Server with a Jersey Servlet. Anyway when I start Jetty and visit a REST resource (in my case: http://192.168.1.12:8080/api/hello) I get a ContainerException with message: The ResourceConfig instance does not contain any root resource classes. (see exception stack trace below).
Any idea why?
MORE DETAILS:
Logcat is giving the following SEVER WARNINGS.
The following errors and warnings have been detected with resource and/or provider classes:
SEVERE: Missing dependency for field: private java.lang.ThreadLocal com.sun.jersey.server.impl.container.servlet.JSPTemplateProcessor.requestInvoker
SEVERE: Missing dependency for field: private java.lang.ThreadLocal com.sun.jersey.server.impl.container.servlet.JSPTemplateProcessor.requestInvoker
This is strange cause java.lang.ThreadLocal is available for Android and HttpServletRequest and HttpServletResponse should be available since I've included servlet-api-2.5.jar in the libs folder.
Jersey is dependent on some javax libraries (jaxb-api-2.2.2.jar,jndi-1.2.1.jar,stax-api-1.0-2.jar) which I had to add to project and set the --core-library parameter temporary to ignore dex warning about javax packages as dependencies.
I also removed the following classes (RenderedImageProvider,DataSourceProvider,MimeMultipartProvider from package com.sun.jersey.core.impl.provider.entity from jersey core jar) to avoid dependencies on java.awt and javax.mail.
EXCEPTION trace:
javax.servlet.UnavailableException: com.sun.jersey.api.container.ContainerException: The ResourceConfig instance does not contain any root resource classes.
at org.eclipse.jetty.servlet.ServletHolder.makeUnavailable(ServletHolder.java:409)
at org.eclipse.jetty.servlet.ServletHolder.initServlet(ServletHolder.java:450)
at org.eclipse.jetty.servlet.ServletHolder.getServlet(ServletHolder.java:331)
at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:476)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:119)
at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:517)
at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:226)
at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:935)
at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:404)
at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:184)
at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:870)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:117)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:116)
at org.eclipse.jetty.server.Server.handle(Server.java:346)
at org.eclipse.jetty.server.HttpConnection.handleRequest(HttpConnection.java:596)
at org.eclipse.jetty.server.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:1051)
at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:592)
at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:214)
at org.eclipse.jetty.server.HttpConnection.handle(HttpConnection.java:426)
at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:520)
at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:40)
at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:528)
at java.lang.Thread.run(Thread.java:1019)
Start Server Android Activity:
public class StartServerActivity extends Activity {
private Server webServer;
private final static String LOG_TAG = "Jetty";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
System.setProperty("java.net.preferIPv4Stack", "true");
System.setProperty("java.net.preferIPv6Addresses", "false");
webServer = new Server(8080);
ServletHolder servletHolder = new ServletHolder(com.sun.jersey.spi.container.servlet.ServletContainer.class);
servletHolder.setInitParameter("com.sun.jersey.config.property.packages", "com.famenu.server.resources");
ServletContextHandler servletContextHandler = new ServletContextHandler(webServer, "/api", true, false);
servletContextHandler.addServlet(servletHolder, "/hello");
webServer.setHandler(servletContextHandler);
try {
webServer.start();
Log.d(LOG_TAG, "started Web server");
}
catch (Exception e) {
Log.d(LOG_TAG, "unexpected exception starting Web server: " + e);
}
}
}
Jersey Resource:
package com.famenu.server.resources;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
#Path("/")
public class HelloResource {
#GET
#Produces("text/plain")
public String getMsg() {
return "Hello Resource";
}
}
I'm using Jetty 7.3.0.v20110203 , Jersey 1.12 , Android 1.6
I arrived till this point after another exception explained here
don't use package/any other scanning on .. not supported platforms.
Classnames property: com.sun.jersey.config.property.classnames should work for you (you need to explicitly declare all your root resource (#Path annotated) classes).

Categories

Resources