I'm trying to use reflection for loadUrl in a webview, but it doesn't works.
I use this code :
Class primary
{..............
public void start(InputStream is)
{
prova = new _JsonRead();
try {
Class<?> test=Class.forName("xxx.test.learning.Webview1");
Field f=test.getDeclaredField("myWebView");
Class<?> web=WebView.class;
Method metodo=web.getMethod("loadUrl",String.class); //restituisce il metodo loadUrl con parametro tipo stringa
;
metodo.invoke(f, "www.google.com");
} catch (Exception e) {
// TODO Auto-generated catch block
Log.i("ecc", "error "+e);
e.printStackTrace();
}
.......
}
public class Webview1 extends Activity
{
public WebView myWebView = null;
public WebSettings webSettings = null;
@Override
public void onCreate(Bundle SavedInstance)
{
super.onCreate(SavedInstance);
setContentView(R.layout.activity_webview);
myWebView = (WebView) findViewById(R.id.webview);
webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
Primary prova = new Primary();
prova.start(getResources().openRawResource(R.raw.config));
//myWebView.loadUrl(prova.indirizzo);
/*
* I move this function
* myWebView.loadUrl("file:///android_asset/test.html");
* in the Learning_lib2
*/
}
}
But there is this error : "03-01 15:58:07.200: I/ecc(19919): error java.lang.IllegalArgumentException: expected receiver of type android.webkit.WebView, but got java.lang.reflect.Field "
I'm new in reflection, and this is my first test. I have thought that Field was a generic Object , but securely I'm wronging..but where ? :/
Aucun commentaire:
Enregistrer un commentaire