在android webview崩溃应用程序中加载栏

时间:2013-10-01 14:00:25

标签: android android-webview

我正在开发此应用以显示特定网页的网页视图。意图启动时将加载此页面。我想添加一个我从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;
    }
}
}

2 个答案:

答案 0 :(得分:0)

以下是在webview上创建加载栏的示例。使用%

的文本
package com.example.webview;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class WebviewActivity extends Activity {

    /** onCreate() is called at start of activity */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final WebView webview = new WebView(this);
        setContentView(webview);


        webview.getSettings().setJavaScriptEnabled(true);
        final Activity activity = this;
        webview.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress) {
                // Activities and WebViews measure progress differently
                // The indicator disappears when 100% is reached
                Toast.makeText(activity, Integer.toString(progress) + "%", Toast.LENGTH_SHORT).show();
              }
            });
        webview.setWebViewClient(new WebViewClient() {
          public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            Toast.makeText(activity, "Error! " + description, Toast.LENGTH_SHORT).show();
          }



          public void onPageFinished(WebView view, String url)  
             {  
                 Toast.makeText(activity, url, Toast.LENGTH_SHORT).show();
             }
        });
         webview.loadUrl("http://gmaps-samples.googlecode.com/svn/trunk/articles-android-webmap/simple-android-map.html");

   }
}

答案 1 :(得分:0)

尝试以下代码。

public class StandardWebView extends Activity {

    // Declare Variables
    WebView webview;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Prepare the progress bar
        requestWindowFeature(Window.FEATURE_PROGRESS);
        // Get the view from webview.xml
        setContentView(R.layout.webview);

        // Locate the WebView in webview.xml
        webview = (WebView) findViewById(R.id.webview);

        // Enable Javascript to run in WebView
        webview.getSettings().setJavaScriptEnabled(true);

        // Allow Zoom in/out controls
        webview.getSettings().setBuiltInZoomControls(true);

        // Zoom out the best fit your screen
        webview.getSettings().setLoadWithOverviewMode(true);
        webview.getSettings().setUseWideViewPort(true);

        // Load URL
        webview.loadUrl("http://www.yoururl.com");

        // Show the progress bar
        webview.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress) {
                setProgress(progress * 100);
            }
        });

        // Call private class InsideWebViewClient
        webview.setWebViewClient(new InsideWebViewClient());

    }

    private class InsideWebViewClient extends WebViewClient {
        @Override
        // Force links to be opened inside WebView and not in Default Browser
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;

        }

    }

}

来源:http://www.androidbegin.com/tutorial/implementing-webview-in-android/