I'm trying to create a customized WebView that overrides a onScrollChanged method. But when I try to instantiate it I get the following error:
06-13 17:30:09.977: E/AndroidRuntime(20366): FATAL EXCEPTION: main
06-13 17:30:09.977: E/AndroidRuntime(20366): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{br.com.facilit.target.app.android/br.com.facilit.target.app.android.BrowserActivity}: java.lang.NullPointerException
06-13 17:30:09.977: E/AndroidRuntime(20366): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1880)
06-13 17:30:09.977: E/AndroidRuntime(20366): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
06-13 17:30:09.977: E/AndroidRuntime(20366): at android.app.ActivityThread.access$600(ActivityThread.java:123)
06-13 17:30:09.977: E/AndroidRuntime(20366): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
06-13 17:30:09.977: E/AndroidRuntime(20366): at android.os.Handler.dispatchMessage(Handler.java:99)
06-13 17:30:09.977: E/AndroidRuntime(20366): at android.os.Looper.loop(Looper.java:137)
06-13 17:30:09.977: E/AndroidRuntime(20366): at android.app.ActivityThread.main(ActivityThread.java:4424)
06-13 17:30:09.977: E/AndroidRuntime(20366): at java.lang.reflect.Method.invokeNative(Native Method)
06-13 17:30:09.977: E/AndroidRuntime(20366): at java.lang.reflect.Method.invoke(Method.java:511)
06-13 17:30:09.977: E/AndroidRuntime(20366): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-13 17:30:09.977: E/AndroidRuntime(20366): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-13 17:30:09.977: E/AndroidRuntime(20366): at dalvik.system.NativeStart.main(Native Method)
06-13 17:30:09.977: E/AndroidRuntime(20366): Caused by: java.lang.NullPointerException
06-13 17:30:09.977: E/AndroidRuntime(20366): at android.content.ContextWrapper.getResources(ContextWrapper.java:81)
06-13 17:30:09.977: E/AndroidRuntime(20366): at android.view.View.<init>(View.java:2696)
06-13 17:30:09.977: E/AndroidRuntime(20366): at android.view.View.<init>(View.java:2744)
06-13 17:30:09.977: E/AndroidRuntime(20366): at android.view.ViewGroup.<init>(ViewGroup.java:385)
06-13 17:30:09.977: E/AndroidRuntime(20366): at android.widget.AbsoluteLayout.<init>(AbsoluteLayout.java:52)
06-13 17:30:09.977: E/AndroidRuntime(20366): at android.webkit.WebView.<init>(WebView.java:1119)
06-13 17:30:09.977: E/AndroidRuntime(20366): at android.webkit.WebView.<init>(WebView.java:1102)
06-13 17:30:09.977: E/AndroidRuntime(20366): at android.webkit.WebView.<init>(WebView.java:1091)
06-13 17:30:09.977: E/AndroidRuntime(20366): at android.webkit.WebView.<init>(WebView.java:1081)
06-13 17:30:09.977: E/AndroidRuntime(20366): at android.webkit.WebView.<init>(WebView.java:1072)
06-13 17:30:09.977: E/AndroidRuntime(20366): at br.com.facilit.target.app.android.CustomizedWebView.<init>(CustomizedWebView.java:12)
06-13 17:30:09.977: E/AndroidRuntime(20366): at br.com.facilit.target.app.android.BrowserActivity.<init>(BrowserActivity.java:29)
06-13 17:30:09.977: E/AndroidRuntime(20366): at java.lang.Class.newInstanceImpl(Native Method)
06-13 17:30:09.977: E/AndroidRuntime(20366): at java.lang.Class.newInstance(Class.java:1319)
06-13 17:30:09.977: E/AndroidRuntime(20366): at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
06-13 17:30:09.977: E/AndroidRuntime(20366): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1871)
06-13 17:30:09.977: E/AndroidRuntime(20366): ... 11 more
My customized webview code:
public class CustomizedWebView extends WebView {
public CustomizedWebView(Context context) {
super(context);
}
public CustomizedWebView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Log.d(Constants.BROWSER_ACTIVITY, "SCROLL CHANGED");
super.onScrollChanged(l, t, oldl, oldt);
}
}
How do I instantiate it:
public class BrowserActivity extends Activity {
private MetaDados metaDado;
String url;
CustomizedWebView webView = new CustomizedWebView(this);
ProgressBar progressUrl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_browser);
final ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
progressUrl = (ProgressBar) findViewById(R.id.progressUrl);
Bundle extras = getIntent().getExtras();
if (extras != null) {
metaDado = extras.getParcelable("metaDado");
}
TextView browserTitle = (TextView) findViewById(R.id.browser_title);
browserTitle.setText(metaDado.getName());
webView = (CustomizedWebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setSupportZoom(true);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setLoadWithOverviewMode(true);
if (metaDado.getType().equals(Constants.FULL)) {
url = "file://" + Constants.DEST_PATH_PARENT + "/" + metaDado.getUuidInstituicao() + "/full/"
+ metaDado.getOfflineUuid() + "/index.html";
} else if (metaDado.getType().equals(Constants.PRES)) {
url = "file://" + Constants.DEST_PATH_PARENT + "/" + metaDado.getUuidInstituicao() + "/pres/"
+ metaDado.getOfflineUuid() + "/" + metaDado.getOfflineUuid() + "/index.html";
}
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
progressUrl.setVisibility(View.VISIBLE);
super.onPageStarted(view, url, favicon);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressUrl.setVisibility(View.GONE);
}
});
webView.loadUrl(url);
addListeners();
}
private void addListeners() {
Button goBack = (Button) findViewById(R.id.back);
Button goForward = (Button) findViewById(R.id.forward);
goForward.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (webView.canGoForward()) {
webView.goForward();
}
}
});
goBack.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (webView.canGoBack()) {
webView.goBack();
}
}
});
}
}
My xml file:
<br.com.facilit.target.app.android.CustomizedWebView
android:id="@+id/webView1"
android:layout_below="@id/header"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
/>