How to properly name native functions when using the NDK - android

I am trying to run my application, but whenever I make a call to a native function it gives me an error and the program crashes. I'm sure it has to do with how I am naming something either in Java or in C.
Here is my call to the functions in Java :
package my.commander;
public class RelayAPIModel {
public static class NativeCalls {
static {
System.loadLibrary( "RelayAPI");
}
public native static byte InitRelayJava();
public native static void FreeRelayJava();
}
Here are the functions in the .c file:
void Java_my_commander_RelayAPIModel_FreeRelayJava( JNIEnv * env, jobject this ) {
RelayAPI_DataValid = 0;
RelayAPI_SetBaud = 0;
RelayAPI_get = 0;
RelayAPI_put = 0;
RelayAPI_flush = 0;
RelayAPI_delay = 0;
RelayAPI_initilized = 0;
}
BYTE Java_my_commander_RelayAPIModel_InitRelayJava( JNIEnv *env, jobject obj ) {
...
...
}
Here they are in the .h file :
void Java_my_commander_RelayAPIModel_FreeRelayJava( JNIEnv * env, jobject obj );
BYTE Java_my_commander_RelayAPIModel_InitRelayJava( JNIEnv *env, jobject obj );
Here is my LogCat:
08-01 09:58:21.933: E/AndroidRuntime(17170): FATAL EXCEPTION: main
08-01 09:58:21.933: E/AndroidRuntime(17170): java.lang.UnsatisfiedLinkError: InitRelayJava
08-01 09:58:21.933: E/AndroidRuntime(17170): at my.eti.commander.RelayAPIModel$NativeCalls.InitRelayJava(Native Method)
08-01 09:58:21.933: E/AndroidRuntime(17170): at my.eti.commander.MainMenu.initMain(MainMenu.java:241)
08-01 09:58:21.933: E/AndroidRuntime(17170): at my.eti.commander.MainMenu.onCreate(MainMenu.java:81)
08-01 09:58:21.933: E/AndroidRuntime(17170): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-01 09:58:21.933: E/AndroidRuntime(17170): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
08-01 09:58:21.933: E/AndroidRuntime(17170): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
08-01 09:58:21.933: E/AndroidRuntime(17170): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-01 09:58:21.933: E/AndroidRuntime(17170): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
08-01 09:58:21.933: E/AndroidRuntime(17170): at android.os.Handler.dispatchMessage(Handler.java:99)
08-01 09:58:21.933: E/AndroidRuntime(17170): at android.os.Looper.loop(Looper.java:130)
08-01 09:58:21.933: E/AndroidRuntime(17170): at android.app.ActivityThread.main(ActivityThread.java:3683)
08-01 09:58:21.933: E/AndroidRuntime(17170): at java.lang.reflect.Method.invokeNative(Native Method)
08-01 09:58:21.933: E/AndroidRuntime(17170): at java.lang.reflect.Method.invoke(Method.java:507)
08-01 09:58:21.933: E/AndroidRuntime(17170): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-01 09:58:21.933: E/AndroidRuntime(17170): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-01 09:58:21.933: E/AndroidRuntime(17170): at dalvik.system.NativeStart.main(Native Method)
I would like to clarify now, that I have tried changing the C method names to Java_my_commander_RelayAPIModel_NativeCalls_FreeRelayJava and Java_my_commander_RelayAPIModel_NativeCalls_InitRelayJava. The application still failed to launch and this was the LogCat for that:
08-01 11:22:10.735: E/AndroidRuntime(17441): FATAL EXCEPTION: main
08-01 11:22:10.735: E/AndroidRuntime(17441): java.lang.UnsatisfiedLinkError: InitRelayJava
08-01 11:22:10.735: E/AndroidRuntime(17441): at my.eti.commander.RelayAPIModel$NativeCalls.InitRelayJava(Native Method)
08-01 11:22:10.735: E/AndroidRuntime(17441): at my.eti.commander.MainMenu.initMain(MainMenu.java:241)
08-01 11:22:10.735: E/AndroidRuntime(17441): at my.eti.commander.MainMenu.onCreate(MainMenu.java:81)
08-01 11:22:10.735: E/AndroidRuntime(17441): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-01 11:22:10.735: E/AndroidRuntime(17441): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
08-01 11:22:10.735: E/AndroidRuntime(17441): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
08-01 11:22:10.735: E/AndroidRuntime(17441): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-01 11:22:10.735: E/AndroidRuntime(17441): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
08-01 11:22:10.735: E/AndroidRuntime(17441): at android.os.Handler.dispatchMessage(Handler.java:99)
08-01 11:22:10.735: E/AndroidRuntime(17441): at android.os.Looper.loop(Looper.java:130)
08-01 11:22:10.735: E/AndroidRuntime(17441): at android.app.ActivityThread.main(ActivityThread.java:3683)
08-01 11:22:10.735: E/AndroidRuntime(17441): at java.lang.reflect.Method.invokeNative(Native Method)
08-01 11:22:10.735: E/AndroidRuntime(17441): at java.lang.reflect.Method.invoke(Method.java:507)
08-01 11:22:10.735: E/AndroidRuntime(17441): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-01 11:22:10.735: E/AndroidRuntime(17441): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-01 11:22:10.735: E/AndroidRuntime(17441): at dalvik.system.NativeStart.main(Native Method)
Now, I think that the error lies in the code above. If you all think there is nothing wrong with it, I'm going to add some more information down here just in case the problem lies elsewhere.
This application that I'm creating, uses the functions above to call a library that has been being used for a while. This library was previously used to call functions in a Palm Pilot program which utilize the Palm's bluetooth capabilities. Since Android devices have different bluetooth libraries/capabilities, I am adding code to the native library to call BACK to the java code, to access the Android device's bluetooth capabilities. I do not want to post all of the code pertaining to this here, but if someone feels that it needs to be posted I will.

The error message says it's at:
my.eti.commander.RelayAPIModel$NativeCalls.InitRelayJava (Native Method)
So make sure that:
the native method declaration is indeed inside the RelayAPIModel class, not insude the RelayAPIModel.NativeCalls nested class.
the package name on Java side is my.commander as opposed to my.eti.commander
EDIT: or you can fix that on the C side. If you want to create native methods in the nested class, the proper name for it would be:
Java_my_commander_RelayAPIModel_00024NativeCalls_InitRelayJava()
The 00024 is the code of the $ character, which is the nested class delimiter in Java internals. Also, the meaning of the of second parameter (jobject Obj) will be different - instead of this pointer/class pointer for RelayAPIModel, it will be the one for the RelayAPIModel.NativeCalls. You won't be able to use it to resolve/call methods in the RelayAPIModel.

You can use javah to generate a .h file with the correctly named native files:
javah -jni -classpath bin/classes com.example.app.MyClass

Related

Unable to resolve superclass of Landroid/support/v4/app/FragmentActivity

I have a project which uses a WelcomeActivity which extends FragmentActivity. I use the support library, so android-support-v4.jar is in the libs folder. When I run this app, there are no problems.
However, I want to add ActionBarSherlock to the project. In the ABS project I use actionbarsherlock-plugin-maps-4.1.0.jar and android-support-v4-r6-googlemaps.jar in the libs folder. In my app project I add the ABS library, and when I try to run the app, this error occurs:
UNEXPECTED TOP-LEVEL EXCEPTION:
java.lang.IllegalArgumentException: already added: Landroid/support/v4/accessibilityservice/AccessibilityServiceInfoCompat$AccessibilityServiceInfoStubImpl;
To solve this, I removed the android-support-v4.jar from my app's libs folder. When I now start my app, the app crashes with this logcat:
08-01 18:59:11.182: W/dalvikvm(13338): Unable to resolve superclass of Landroid/support/v4/app/FragmentActivity; (620)
08-01 18:59:11.182: W/dalvikvm(13338): Link of class 'Landroid/support/v4/app/FragmentActivity;' failed
08-01 18:59:11.182: W/dalvikvm(13338): Unable to resolve superclass of Lcom/myapp/welcome/WelcomeActivity; (109)
08-01 18:59:11.182: W/dalvikvm(13338): Link of class 'Lcom/myapp/welcome/WelcomeActivity;' failed
08-01 18:59:11.182: E/dalvikvm(13338): Could not find class 'com.myapp.welcome.WelcomeActivity', referenced from method com.myapp.MainDispatcherActivity.startWelcomeActivity
08-01 18:59:11.182: W/dalvikvm(13338): VFY: unable to resolve const-class 690 (Lcom/myapp/welcome/WelcomeActivity;) in Lcom/myapp/MainDispatcherActivity;
08-01 18:59:11.182: D/dalvikvm(13338): VFY: replacing opcode 0x1c at 0x0002
08-01 18:59:11.182: D/dalvikvm(13338): VFY: dead code 0x0004-000b in Lcom/myapp/MainDispatcherActivity;.startWelcomeActivity ()V
08-01 18:59:11.182: D/AndroidRuntime(13338): Shutting down VM
08-01 18:59:11.182: W/dalvikvm(13338): threadid=1: thread exiting with uncaught exception (group=0x40015560)
08-01 18:59:11.182: E/AndroidRuntime(13338): FATAL EXCEPTION: main
08-01 18:59:11.182: E/AndroidRuntime(13338): java.lang.NoClassDefFoundError: com.myapp.welcome.WelcomeActivity
08-01 18:59:11.182: E/AndroidRuntime(13338): at com.myapp.MainDispatcherActivity.startWelcomeActivity(MainDispatcherActivity.java:33)
08-01 18:59:11.182: E/AndroidRuntime(13338): at com.myapp.MainDispatcherActivity.startProperActivity(MainDispatcherActivity.java:26)
08-01 18:59:11.182: E/AndroidRuntime(13338): at com.myapp.MainDispatcherActivity.onCreate(MainDispatcherActivity.java:19)
08-01 18:59:11.182: E/AndroidRuntime(13338): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-01 18:59:11.182: E/AndroidRuntime(13338): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
08-01 18:59:11.182: E/AndroidRuntime(13338): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
08-01 18:59:11.182: E/AndroidRuntime(13338): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-01 18:59:11.182: E/AndroidRuntime(13338): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
08-01 18:59:11.182: E/AndroidRuntime(13338): at android.os.Handler.dispatchMessage(Handler.java:99)
08-01 18:59:11.182: E/AndroidRuntime(13338): at android.os.Looper.loop(Looper.java:130)
08-01 18:59:11.182: E/AndroidRuntime(13338): at android.app.ActivityThread.main(ActivityThread.java:3683)
08-01 18:59:11.182: E/AndroidRuntime(13338): at java.lang.reflect.Method.invokeNative(Native Method)
08-01 18:59:11.182: E/AndroidRuntime(13338): at java.lang.reflect.Method.invoke(Method.java:507)
08-01 18:59:11.182: E/AndroidRuntime(13338): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:862)
08-01 18:59:11.182: E/AndroidRuntime(13338): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
08-01 18:59:11.182: E/AndroidRuntime(13338): at dalvik.system.NativeStart.main(Native Method)
How can I fix this?
Edit
I have another project which uses the same ABS library, and runs fine. I have no idea what I've done differently, or which settings are wrong.
It seems this error occurred because I didn't have this line in the AndroidManifest.xml file, in the <application> tag:
<uses-library android:name="com.google.android.maps" />
Click here
I think it is the same problem at the Build Path-> Configure Build Path -> Order and Export, the "android-support-v4.jar" needs to be checked.
Hope it is useful to you.
Have you set the compiler compliance for the actionbarsherlock library project? Right click on the library project, go to properties. Under java compiler, make sure compliance is set to 1.6. I had a similar issue, and found the compliance set to 1.5.
I haven't run into it myself, but I think the doc for ActionBarSherlock says you need to extend SherlockFragmentActivity rather than FragmentActivity. It may not be the problem, but it's a problem.

OPEN FEINT error in Android App

15 reports
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.oas.fruitkungfufree/com.openfeint.internal.ui.IntroFlow}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
at android.app.ActivityThread.access$2300(ActivityThread.java:125)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.openfeint.internal.Util.setOrientation(Util.java:36)
at com.openfeint.internal.ui.NestedWindow.onCreate(NestedWindow.java:32)
at com.openfeint.internal.ui.WebNav.onCreate(WebNav.java:93)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
Does any one have a solution for this??
This is a problem with your code, you can't use an object that is null. This is a hint.

NullPointerException in com.reg.project

I have no Idea why I get this error :/
Exception class java.lang.NullPointerException
Source method Jax$7.onClick()
java.lang.NullPointerException
at com.reg.lolsoundboard.Jax$7.onClick(Jax.java:83)
at android.view.View.performClick(View.java:2485)
at android.view.View$PerformClick.run(View.java:9089)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3806)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
Obviously you are dereferencing a variable in line 83 of the file Jax.java where the variable is null.
java.lang.NullPointerException
at com.reg.lolsoundboard.Jax$7.onClick(Jax.java:83)
If show the relevant code, people may be able to help you.

NetworkConnectivityListener class not found on UPC300-2.2 Viewsonic G-Tablet

I am getting this error in my application, and this happens only on UPC300-2.2 Viewsonic G-Tablet. All answers I've found so far (including this one) adviced on putting
<uses-permission android:name="android.permission.INTERNET"/>
to the manifest file, but it doesn't help.
Here is the complete stacktrace:
java.lang.NoClassDefFoundError: android.net.NetworkConnectivityListener
at
com.google.android.maps.MapActivity.onCreate(MapActivity.java:199)
at
myapp.Activity.ActivityMapSearch.onCreate(ActivityMapSearch.java:99)
at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
at
android.app.ActivityThread.access$2300(ActivityThread.java:125)
at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
at
android.os.Handler.dispatchMessage(Handler.java:99)
at
android.os.Looper.loop(Looper.java:123)
at
android.app.ActivityThread.main(ActivityThread.java:4627)
at
java.lang.reflect.Method.invokeNative(Native
Method) at
java.lang.reflect.Method.invoke(Method.java:521)
at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at
dalvik.system.NativeStart.main(Native
Method)
If that is TRUE, you can grab the source code of the class from the Android Source and add it yourself

Cannot use a Tablelayout within a widget?

Here is the xml file of my Android widget:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableLayout
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="fill_parent"
android:stretchColumns="1">
<TableRow>
<!-- Some views -->
</TableRow>
<TableRow>
<!-- Some views -->
</TableRow>
</TableLayout>
</LinearLayout>
(full code here: http://pastebin.com/Kqxs5t9E)
This code is showing me some good results in Eclipse, but as soon as I sent the file to my device and try to add the widget to my homescreen I get this error code and the widget showing: "Problem Loading the Widget":
08-01 22:11:26.917: WARN/AppWidgetHostView(5860): updateAppWidget couldn't find any view, using error view
08-01 22:11:26.917: WARN/AppWidgetHostView(5860): android.view.InflateException: Binary XML file line #20: Error inflating class android.widget.TableLayout
08-01 22:11:26.917: WARN/AppWidgetHostView(5860): at android.view.LayoutInflater.createView(LayoutInflater.java:513)
08-01 22:11:26.917: WARN/AppWidgetHostView(5860): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
08-01 22:11:26.917: WARN/AppWidgetHostView(5860): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
08-01 22:11:26.917: WARN/AppWidgetHostView(5860): at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
08-01 22:11:26.917: WARN/AppWidgetHostView(5860): at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
08-01 22:11:26.917: WARN/AppWidgetHostView(5860): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
08-01 22:11:26.917: WARN/AppWidgetHostView(5860): at android.widget.RemoteViews.apply(RemoteViews.java:930)
08-01 22:11:26.917: WARN/AppWidgetHostView(5860): at android.appwidget.AppWidgetHostView.updateAppWidget(AppWidgetHostView.java:219)
08-01 22:11:26.917: WARN/AppWidgetHostView(5860): at android.appwidget.AppWidgetHostView.updateAppWidget(AppWidgetHostView.java:155)
08-01 22:11:26.917: WARN/AppWidgetHostView(5860): at android.appwidget.AppWidgetHost.createView(AppWidgetHost.java:218)
08-01 22:11:26.917: WARN/AppWidgetHostView(5860): at com.fede.launcher.Launcher.bindAppWidgets(Launcher.java:3652)
08-01 22:11:26.917: WARN/AppWidgetHostView(5860): at com.fede.launcher.Launcher.access$22(Launcher.java:3639)
08-01 22:11:26.917: WARN/AppWidgetHostView(5860): at com.fede.launcher.Launcher$DesktopBinder.handleMessage(Launcher.java:4625)
08-01 22:11:26.917: WARN/AppWidgetHostView(5860): at android.os.Handler.dispatchMessage(Handler.java:99)
08-01 22:11:26.917: WARN/AppWidgetHostView(5860): at android.os.Looper.loop(Looper.java:123)
08-01 22:11:26.917: WARN/AppWidgetHostView(5860): at android.app.ActivityThread.main(ActivityThread.java:4627)
08-01 22:11:26.917: WARN/AppWidgetHostView(5860): at java.lang.reflect.Method.invokeNative(Native Method)
08-01 22:11:26.917: WARN/AppWidgetHostView(5860): at java.lang.reflect.Method.invoke(Method.java:521)
08-01 22:11:26.917: WARN/AppWidgetHostView(5860): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-01 22:11:26.917: WARN/AppWidgetHostView(5860): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-01 22:11:26.917: WARN/AppWidgetHostView(5860): at dalvik.system.NativeStart.main(Native Method)
08-01 22:11:26.917: WARN/AppWidgetHostView(5860): Caused by: android.view.InflateException: Binary XML file line #20: Class not allowed to be inflated android.widget.TableLayout
08-01 22:11:26.917: WARN/AppWidgetHostView(5860): at android.view.LayoutInflater.failNotAllowed(LayoutInflater.java:525)
08-01 22:11:26.917: WARN/AppWidgetHostView(5860): at android.view.LayoutInflater.createView(LayoutInflater.java:472)
08-01 22:11:26.917: WARN/AppWidgetHostView(5860): ... 20 more
So what? I cannot use a Tablelayout within a widget????
What is the best solution so? I can create a lot of relativelayout but I guess that's not a good idea.
I cannot use a Tablelayout within a
widget????
If by "widget" you mean "app widget", then, no, you cannot use a TableLayout in an app widget. There are only a few widgets that can be used in an app widget.
I can create a lot of relativelayout
but I guess that's not a good idea.
I do not know why you think RelativeLayout is bad. You may be able to replace your LinearLayout, TableLayout, and TableRow widgets with a single RelativeLayout.

Categories

Resources