我正在开发这个应用程序来显示特定页面的 web 视图。此页面将在 Intent 启动时加载。我想添加一个我从 android 教程 ( http://developer.android.com/reference/android/webkit/WebView.html ) 中看到的进度条,但由于某种原因,它现在在创建此意图时使我的应用程序崩溃。有什么建议么?谢谢!
这是我在 logcat 中的错误消息:
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
新制作的崩溃代码:
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);
}
}
这段代码是我在将其编辑为上面的新代码之前开始的:
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;
}
}
}