0

我正在使用 Android 中的网络视图,并尝试添加进度条,这是我的代码:

case R.id.studentsite:
            getWindow().requestFeature(Window.FEATURE_PROGRESS);
            setContentView(R.layout.webview);
            WebView wv1 = (WebView)findViewById(R.id.webview);
            WebSettings ws1 = wv1.getSettings();
            final Activity activity = this;
            wv1.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);
              }
            });
            wv1.setWebViewClient(new WebViewClient() {
              public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
              }
            });
            wv1.getSettings().setBuiltInZoomControls(true);
            ws1.setJavaScriptEnabled(true);
            wv1.setWebViewClient(new WebViewClient());
            wv1.loadUrl("http://www.studentsite.gunadarma.ac.id");
            break;

问题是我在 LOGCAT 上遇到了这样的异常:

09-17 13:51:34.487: ERROR/AndroidRuntime(1331): FATAL EXCEPTION: main 09-17 13:51:34.487: ERROR/AndroidRuntime(1331): android.util.AndroidRuntimeException: requestFeature() must be called before adding content 09-17 13:51:34.487: ERROR/AndroidRuntime(1331): at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:181) 09-17 13:51:34.487: ERROR/AndroidRuntime(1331): at com.ugsimplify.ugweb.callintent(ugweb.java:89) 09-17 13:51:34.487: ERROR/AndroidRuntime(1331): at com.ugsimplify.ugweb$1.onClick(ugweb.java:29) 09-17 13:51:34.487: ERROR/AndroidRuntime(1331): at android.view.View.performClick(View.java:2485) 09-17 13:51:34.487: ERROR/AndroidRuntime(1331): at android.widget.CompoundButton.performClick(CompoundButton.java:99) 09-17 13:51:34.487: ERROR/AndroidRuntime(1331): at android.view.View$PerformClick.run(View.java:9080) 09-17 13:51:34.487: ERROR/AndroidRuntime(1331): at android.os.Handler.handleCallback(Handler.java:587) 09-17 13:51:34.487: ERROR/AndroidRuntime(1331): at android.os.Handler.dispatchMessage(Handler.java:92) 09-17 13:51:34.487: ERROR/AndroidRuntime(1331): at android.os.Looper.loop(Looper.java:123) 09-17 13:51:34.487: ERROR/AndroidRuntime(1331): at android.app.ActivityThread.main(ActivityThread.java:3647) 09-17 13:51:34.487: ERROR/AndroidRuntime(1331): at java.lang.reflect.Method.invokeNative(Native Method) 09-17 13:51:34.487: ERROR/AndroidRuntime(1331): at java.lang.reflect.Method.invoke(Method.java:507) 09-17 13:51:34.487: ERROR/AndroidRuntime(1331): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 09-17 13:51:34.487: ERROR/AndroidRuntime(1331): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 09-17 13:51:34.487: ERROR/AndroidRuntime(1331): at dalvik.system.NativeStart.main(Native Method)

你知道如何解决它吗?谢谢。

4

3 回答 3

1

Oncreate之后在下面的行中调用super.oncreate

getWindow().requestFeature(Window.FEATURE_PROGRESS);

您在按钮单击中调用它,您已经为其设置了内容。进度条功能只能在第一次 setContentView 之前请求。

于 2012-09-17T07:06:08.623 回答
0
    Hi Here is problem that you have call.


  getWindow().requestFeature(Window.FEATURE_PROGRESS); before  setContentView(R.layout.webview);
    It should be call below the setContentView(R.layout.webview);

    Answer :-


                setContentView(R.layout.webview);
    getWindow().requestFeature(Window.FEATURE_PROGRESS);
于 2012-09-17T07:05:49.837 回答
0

根据您在设置 contentView 后使用 requestFeature 的 logcat。

您发布的代码部分是完美的。

我怀疑您已经在其他地方使用过 setContentView,也许是在 onCreate 方法中。

于 2012-09-17T07:06:43.350 回答