Note that there are some explanatory texts on larger screens.

plurals
  1. POLoading bar in android webview crashing app
    text
    copied!<p>I am developing this app to display a webview of a specific page. This page will load when the intent is started. Im wanting to add a progress bar which I saw from android tutorials (<a href="http://developer.android.com/reference/android/webkit/WebView.html" rel="nofollow">http://developer.android.com/reference/android/webkit/WebView.html</a>) but for some reason, it now crashes my app when this intent is created. Any suggestions? Thanks!</p> <p>This is my error message in logcat:</p> <pre><code>10-01 13:52:35.679: E/AndroidRuntime(273): FATAL EXCEPTION: main 10-01 13:52:35.679: E/AndroidRuntime(273): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.Depauw.dpuhelpdesk/ com.Depauw.dpuhelpdesk.accounts_activity_mealplan}: android.util.AndroidRuntimeException: requestFeature() must be called before adding content 10-01 13:52:35.679: E/AndroidRuntime(273): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 10-01 13:52:35.679: E/AndroidRuntime(273): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 10-01 13:52:35.679: E/AndroidRuntime(273): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 10-01 13:52:35.679: E/AndroidRuntime(273): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 10-01 13:52:35.679: E/AndroidRuntime(273): at android.os.Handler.dispatchMessage(Handler.java:99) 10-01 13:52:35.679: E/AndroidRuntime(273): at android.os.Looper.loop(Looper.java:123) 10-01 13:52:35.679: E/AndroidRuntime(273): at android.app.ActivityThread.main(ActivityThread.java:4627) 10-01 13:52:35.679: E/AndroidRuntime(273): at java.lang.reflect.Method.invokeNative(Native Method) 10-01 13:52:35.679: E/AndroidRuntime(273): at java.lang.reflect.Method.invoke(Method.java:521) 10-01 13:52:35.679: E/AndroidRuntime(273): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 10-01 13:52:35.679: E/AndroidRuntime(273): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 10-01 13:52:35.679: E/AndroidRuntime(273): at dalvik.system.NativeStart.main(Native Method) 10-01 13:52:35.679: E/AndroidRuntime(273): Caused by: android.util.AndroidRuntimeException: requestFeature() must be called before adding content 10-01 13:52:35.679: E/AndroidRuntime(273): at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:172) 10-01 13:52:35.679: E/AndroidRuntime(273): at com.Depauw.dpuhelpdesk.accounts_activity_mealplan.Initialize(accounts_activity_mealplan.java:35) 10-01 13:52:35.679: E/AndroidRuntime(273): at com.Depauw.dpuhelpdesk.accounts_activity_mealplan.onCreate(accounts_activity_mealplan.java:23) 10-01 13:52:35.679: E/AndroidRuntime(273): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 10-01 13:52:35.679: E/AndroidRuntime(273): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 10-01 13:52:35.679: E/AndroidRuntime(273): ... 11 more </code></pre> <p>NEWLY MADE CODE WHICH CRASHES:</p> <pre><code>package com.Depauw.dpuhelpdesk; import android.annotation.SuppressLint; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.Window; import android.webkit.WebChromeClient; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.Toast; @SuppressLint("SetJavaScriptEnabled") public class accounts_activity_mealplan extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.webview_main); Initialize(); } private void Initialize(){ WebView mainWebView = (WebView) findViewById(R.id.webView1); WebSettings webSettings = mainWebView.getSettings(); webSettings.setJavaScriptEnabled(true); // Let's display the progress in the activity title bar, like the // browser app does. getWindow().requestFeature(Window.FEATURE_PROGRESS); mainWebView.getSettings().setJavaScriptEnabled(true); final Activity activity = this; mainWebView.setWebChromeClient(new WebChromeClient() { public void onProgressChanged(WebView view, int progress) { // Activities and WebViews measure progress with different scales. // The progress meter will automatically disappear when we reach 100% activity.setProgress(progress * 1000); } }); mainWebView.setWebViewClient(new WebViewClient() { public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show(); } }); mainWebView.loadUrl("http://www.depauw.edu/studentlife/campusliving/diningoptions/?plans.html"); mainWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY); } } </code></pre> <p>THIS CODE IS WHAT I STARTED WITH BEFORE EDITING IT TO THE NEW CODE ABOVE:</p> <pre><code>package com.Depauw.dpuhelpdesk; import android.annotation.SuppressLint; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; @SuppressLint("SetJavaScriptEnabled") public class accounts_activity_mealplan extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.webview_main); Initialize(); } private void Initialize(){ WebView mainWebView = (WebView) findViewById(R.id.webView1); WebSettings webSettings = mainWebView.getSettings(); webSettings.setJavaScriptEnabled(true); mainWebView.setWebViewClient(new MyCustomWebViewClient()); mainWebView.loadUrl("http://www.depauw.edu/studentlife/campusliving/diningoptions/?plans.html"); mainWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY); } private class MyCustomWebViewClient extends WebViewClient { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload