Magnus Johansson
Magnus Johansson

Reputation: 28325

Java.Lang.NoSuchFieldError when using Mono for Android and Java-Binding

So I'm trying out the Xamarin Facebook Simple sample which uses Java-Binding.

Facing initial issues, I had to download and compile the latest Facebook for Android SDK, (using Eclipse) and zipped it for the Facebook java-bindings project.

However, when clicking the logout button, I get this error:

Java.Lang.NoSuchFieldError: com.facebook.android.R$drawable.close
  at Android.Runtime.JNIEnv.CallVoidMethod (intptr,intptr,Android.Runtime.JValue[]) [0x00023] in /Users/builder/data/lanes/monodroid-mac-monodroid-4.2.4-branch/74c352d0/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:368
  at Com.Facebook.Android.Facebook.Authorize (Android.App.Activity,string[],Com.Facebook.Android.Facebook/IDialogListener) <IL 0x00095, 0x002af>
  at Com.Facebook.Android.LoginButton/ButtonOnClickListener.OnClick (Android.Views.View) [0x00051] in C:\Users\magnus\Downloads\xamarin-monodroid-samples-7e2c105\xamarin-monodroid-samples-7e2c105\Facebook\samples\simple\simple\Example.cs:140
  at Android.Views.View/IOnClickListenerInvoker.n_OnClick_Landroid_view_View_ (intptr,intptr,intptr) [0x00010] in /Users/builder/data/lanes/monodroid-mac-monodroid-4.2.4-branch/74c352d0/source/monodroid/src/Mono.Android/platforms/android-8/src/generated/Android.Views.View.cs:600
  at (wrapper dynamic-method) object.9681ec74-91e4-4ca3-93b5-489e6e022fb2 (intptr,intptr,intptr) <IL 0x00012, 0x0001b>

  --- End of managed exception stack trace ---
  java.lang.NoSuchFieldError: com.facebook.android.R$drawable.close
    at com.facebook.android.FbDialog.createCrossImage(FbDialog.java:106)
    at com.facebook.android.FbDialog.onCreate(FbDialog.java:81)
    at android.app.Dialog.dispatchOnCreate(Dialog.java:307)
    at android.app.Dialog.show(Dialog.java:225)
    at com.facebook.android.Facebook.dialog(Facebook.java:814)
    at com.facebook.android.Facebook.startDialogAuth(Facebook.java:343)
    at com.facebook.android.Facebook.authorize(Facebook.java:206)
    at com.facebook.android.Facebook.authorize(Facebook.java:125)
    at com.facebook.android.LoginButton_ButtonOnClickListener.n_onClick(Native Method)
    at com.facebook.android.LoginButton_ButtonOnClickListener.onClick(LoginButton_ButtonOnClickListener.java:36)
    at android.view.View.performClick(View.java:2485)
    at android.view.View$PerformClick.run(View.java:9080)
    at android.os.Handler.handleCallback(Handler.java:587)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:130)
    at android.app.ActivityThread.main(ActivityThread.java:3683)
    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)

Using the exact same JAR in an Eclipse Java project works just fine.
It would seem that the R$drawable.close resource is missing, but it's present in the Zip file. Any ideas what could be wrong?

Upvotes: 0

Views: 1004

Answers (1)

Bryan
Bryan

Reputation: 684

Ah yes, this problem. It's a bit of a headache, but if you look in the java code the facebook dialog has the close button being set in code. So when your application compiles it cannot find the close button's id.

A work around for this would be to change the FBDialog.java file to somehow take in your Resource's close integer id generated in the Resource.Designer.cs file.

private void createCrossImage(int value) {
    mCrossImage = new ImageView(getContext());
    // Dismiss the dialog when user click on the 'x'
    mCrossImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mListener.onCancel();
            FbDialog.this.dismiss();
        }
    });
    Drawable crossDrawable = getContext().getResources().getDrawable(value);
    mCrossImage.setImageDrawable(crossDrawable);
    /* 'x' should not be visible while webview is loading
     * make it visible only after webview has fully loaded
    */
    mCrossImage.setVisibility(View.INVISIBLE);
}

I modified the java code and recompiled the .jar file so I could pass in the int value from my Resources file so I wouldn't have to worry about it not being able to find the id of that particular image.

Upvotes: 2

Related Questions