Reputation: 894
Hi I need to get the height of page "http://www.android.com". So why I inject javascript into android . But I have an error in the execution. Can any person to help me.
Code
package com.load.page_html;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class Main extends Activity {
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
final WebView webview = (WebView)findViewById(R.id.share_to_twitter_webView);
/* JavaScript must be enabled if you want it to work, obviously */
webview.getSettings().setJavaScriptEnabled(true);
/* WebViewClient must be set BEFORE calling loadUrl! */
webview.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url)
{
webview.loadUrl("javascript:(function() { if (document.body && document.body.offsetHeight) " +
"return document.body.offsetHeight; " + "})()");
}
});
webview.loadUrl("http://www.android.com");
}
}
The Error
03-18 14:50:31.679: ERROR/AndroidRuntime(462): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.load.page_html/com.load.page_html.Main}: java.lang.NullPointerException
03-18 14:50:31.679: ERROR/AndroidRuntime(462): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
03-18 14:50:31.679: ERROR/AndroidRuntime(462): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
03-18 14:50:31.679: ERROR/AndroidRuntime(462): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
03-18 14:50:31.679: ERROR/AndroidRuntime(462): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
03-18 14:50:31.679: ERROR/AndroidRuntime(462): at android.os.Handler.dispatchMessage(Handler.java:99)
03-18 14:50:31.679: ERROR/AndroidRuntime(462): at android.os.Looper.loop(Looper.java:123)
03-18 14:50:31.679: ERROR/AndroidRuntime(462): at android.app.ActivityThread.main(ActivityThread.java:4363)
03-18 14:50:31.679: ERROR/AndroidRuntime(462): at java.lang.reflect.Method.invokeNative(Native Method)
03-18 14:50:31.679: ERROR/AndroidRuntime(462): at java.lang.reflect.Method.invoke(Method.java:521)
03-18 14:50:31.679: ERROR/AndroidRuntime(462): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
03-18 14:50:31.679: ERROR/AndroidRuntime(462): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
03-18 14:50:31.679: ERROR/AndroidRuntime(462): at dalvik.system.NativeStart.main(Native Method)
03-18 14:50:31.679: ERROR/AndroidRuntime(462): Caused by: java.lang.NullPointerException
03-18 14:50:31.679: ERROR/AndroidRuntime(462): at com.load.page_html.Main.onCreate(Main.java:16)
03-18 14:50:31.679: ERROR/AndroidRuntime(462): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-18 14:50:31.679: ERROR/AndroidRuntime(462): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
Upvotes: 0
Views: 464
Reputation: 8550
I believe you should be using webclient.getContentHeight, which would make your life a lot easier :D
Edit after comments : In order to have this function return a correct value you have to wait until the page is fully loaded which can be monitored by the callback WebClient.onPageFinished()
http://developer.android.com/reference/android/webkit/WebView.html#getContentHeight()
Upvotes: 1
Reputation: 54705
Method findViewById(R.id.share_to_twitter_webView)
returns null
because you forgot to do setContentView(R.layout.main)
.
Upvotes: 1