2011-07-10 105 views
1

我想爲webview設置旋轉輪,直到頁面加載時,我正在使用下面的方法,但它的浮動與以下logcat錯誤。請指導webview的旋轉輪

public class Webview extends Activity { 
    WebView mWebView; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    this.getWindow().requestFeature(Window.FEATURE_PROGRESS); 


    // Makes Progress bar Visible 



    getWindow().requestFeature(Window.FEATURE_PROGRESS); 

    WebView mWebView = (WebView) findViewById(R.id.webview); 

    mWebView.getSettings().setJavaScriptEnabled(true); 

    final Activity activity = this; 

    mWebView.setWebChromeClient(new WebChromeClient(){ 

      public void onProgressChanged(WebView view, int progress) { 
        activity.setTitle("Loading..."); 
        activity.setProgress(progress * 100); 
         if(progress == 100) 
          activity.setTitle("My title"); 
        } 
    }); 

    mWebView.loadUrl(".........................."); 

    mWebView.setVerticalScrollBarEnabled(false); 
    mWebView.setHorizontalScrollBarEnabled(false); 

} 

XML

<?xml version="1.0" encoding="utf-8"?> 


    <WebView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/webview" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 

/> 

logcat的

07-10 17:49:18.785: DEBUG/AndroidRuntime(23772): Shutting down VM 
07-10 17:49:18.785: WARN/dalvikvm(23772): threadid=3: thread exiting with uncaught exception (group=0x4001e390) 
07-10 17:49:18.785: ERROR/AndroidRuntime(23772): Uncaught handler: thread main exiting due to uncaught exception 
07-10 17:49:18.795: ERROR/AndroidRuntime(23772): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.droidnova.android.howto.optionmenu/com.droidnova.android.howto.optionmenu.Webview}: java.lang.NullPointerException 
07-10 17:49:18.795: ERROR/AndroidRuntime(23772):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2596) 
07-10 17:49:18.795: ERROR/AndroidRuntime(23772):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2621) 
07-10 17:49:18.795: ERROR/AndroidRuntime(23772):  at android.app.ActivityThread.access$2200(ActivityThread.java:126) 
07-10 17:49:18.795: ERROR/AndroidRuntime(23772):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1932) 
07-10 17:49:18.795: ERROR/AndroidRuntime(23772):  at android.os.Handler.dispatchMessage(Handler.java:99) 
07-10 17:49:18.795: ERROR/AndroidRuntime(23772):  at android.os.Looper.loop(Looper.java:123) 
07-10 17:49:18.795: ERROR/AndroidRuntime(23772):  at android.app.ActivityThread.main(ActivityThread.java:4595) 
07-10 17:49:18.795: ERROR/AndroidRuntime(23772):  at java.lang.reflect.Method.invokeNative(Native Method) 
07-10 17:49:18.795: ERROR/AndroidRuntime(23772):  at java.lang.reflect.Method.invoke(Method.java:521) 
07-10 17:49:18.795: ERROR/AndroidRuntime(23772):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) 
07-10 17:49:18.795: ERROR/AndroidRuntime(23772):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 
07-10 17:49:18.795: ERROR/AndroidRuntime(23772):  at dalvik.system.NativeStart.main(Native Method) 
07-10 17:49:18.795: ERROR/AndroidRuntime(23772): Caused by: java.lang.NullPointerException 
07-10 17:49:18.795: ERROR/AndroidRuntime(23772):  at com.droidnova.android.howto.optionmenu.Webview.onCreate(Webview.java:36) 
07-10 17:49:18.795: ERROR/AndroidRuntime(23772):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
07-10 17:49:18.795: ERROR/AndroidRuntime(23772):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2544) 
07-10 17:49:18.795: ERROR/AndroidRuntime(23772):  ... 11 more 
+0

在這裏,人們可以找到很好的例子http://www.chrisdanielson.com/2010/05/04/android-webview-and-所述-超靜定正在進行的溶液/ – umar

回答

2

我沒有看到它在你的代碼 - 你應該叫setContentView你可以打電話findViewById之前,否則將返回null 。

所以,如果你的XML被稱爲main.xml中,你應該首先調用setContentView(R.layout.main);