2013-10-01 48 views
-1

我正在開發此應用程序以顯示特定頁面的webview。當意圖啓動時,此頁面將加載。我想添加一個進度條,我看到從Android教程(http://developer.android.com/reference/android/webkit/WebView.html),但由於某種原因,它現在崩潰我的應用程序,當這個意圖創建。有什麼建議麼?謝謝!在android webview崩潰應用程序中加載酒吧

這是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); 
} 
} 

這段代碼是什麼,我開始編輯前IT TO THE NEW以上代碼:

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; 
    } 
} 
} 
+0

我不明白是什麼意思,雖然,對不起,我知道該怎麼聽起來新手。這是我第一次做這樣的事情 – booky99

+0

關於requestfeature()的部分。那是什麼告訴我的?我之前基本上調用了進度條之前什麼都能加載,導致android崩潰? – booky99

+0

檢查文檔... Window.requestFeature(int)... – Selvin

回答

0

下面是一個在web視圖上創建加載欄的示例。使用文字%

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"); 

    } 
} 
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/