我想为webview设置旋转轮,直到页面加载为止,我使用以下方法,但它浮动时出现以下logcat错误。请指导
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 :(得分:2)
我在你的代码中没有看到它 - 你应该在调用findViewById
之前调用setContentView,否则它将返回null。
因此,如果您的xml名为main.xml,则应首先调用setContentView(R.layout.main);