Hello, this is my first post here, so I hope I follow all the guidlines??

I have a problem using the AddJavascriptInterface with a WebView, it
"compiles" and runs the application but the javascript function
window.Testaren.getNumb() isn't there, it is null.
The Test.html page is displayed in the app, like it should.

I'm trying to make the text Test Page! change to the calculated number 10,
but when i click the body the application crasches because Testaren is null.
I'm using an emulator for this.

Here is my code for this project.

My Testar.java file contains..

package com.testar.Totsie;

public class Testar
{
        public Testar()
        {
        }
        public int getNumb(int i)
        {
                return i*2;
        }
}


And my Mono activity class..

public class Activity1 : Activity
{
       
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
           

            // Hide the window title.
            RequestWindowFeature(WindowFeatures.NoTitle);
            SetContentView(Resource.Layout.Main); 

            WebView vv = FindViewById<WebView>(Resource.Id.webView);
            vv.Settings.JavaScriptEnabled = true;
            
            IntPtr JavaScriptInterface_Class =
JNIEnv.FindClass("com/testar/Totsie/Testar");

            IntPtr JavaScriptInterface_ctor =
JNIEnv.GetMethodID(JavaScriptInterface_Class, "<init>", "()V");

            IntPtr instance = JNIEnv.NewObject(JavaScriptInterface_Class,
JavaScriptInterface_ctor);

            vv.AddJavascriptInterface(new Java.Lang.Object(instance),
"Testaren");

            vv.LoadUrl(@"http://www.xxxxxx.se/test.html";);            
       
        }        
}

And javascript to call this function on the Test.html page, as follows..

<html>
<head>
<title>Ruhuset</title>

</head>
<body onclick="javascript:getNumb();" id="body">
Test page!
</body>
</html>

--
View this message in context: 
http://mono-for-android.1047100.n5.nabble.com/Trouble-using-the-AddJavascriptInterface-with-a-WebView-tp4939866p4939866.html
Sent from the Mono for Android mailing list archive at Nabble.com.
_______________________________________________
Monodroid mailing list
[email protected]

UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid

Reply via email to