2012-04-05 98 views
2

我試圖讓我的Android應用程序中的瀏覽器功能正常工作,但應用程序在加載時崩潰。下面是當新的活動被加載代碼:應用程序在加載瀏覽器時崩潰

@Override 
public void onCreate(Bundle savedInstanceState) { 
    WebView webview = new WebView(this); 
    setContentView(webview); 

    webview.loadUrl("http://erik-edgren.nu/weather"); 
    getWindow().requestFeature(Window.FEATURE_PROGRESS); 

    WebSettings webSettings = webview.getSettings(); 
    webSettings.setJavaScriptEnabled(true); 
} 

這裏是logcat的:

04-05 13:51:20.224: D/dalvikvm(742): GC freed 2673 objects/169288 bytes in 107ms 
04-05 13:51:20.354: D/AndroidRuntime(742): Shutting down VM 
04-05 13:51:20.354: W/dalvikvm(742): threadid=3: thread exiting with uncaught exception (group=0x4001b188) 
04-05 13:51:20.364: E/AndroidRuntime(742): Uncaught handler: thread main exiting due to uncaught exception 
04-05 13:51:20.373: E/AndroidRuntime(742): java.lang.RuntimeException: Unable to start activity ComponentInfo{weather.right.now/weather.right.Browser}: android.util.AndroidRuntimeException: requestFeature() must be called before adding content 
04-05 13:51:20.373: E/AndroidRuntime(742): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496) 
04-05 13:51:20.373: E/AndroidRuntime(742): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512) 
04-05 13:51:20.373: E/AndroidRuntime(742): at android.app.ActivityThread.access$2200(ActivityThread.java:119) 
04-05 13:51:20.373: E/AndroidRuntime(742): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863) 
04-05 13:51:20.373: E/AndroidRuntime(742): at android.os.Handler.dispatchMessage(Handler.java:99) 
04-05 13:51:20.373: E/AndroidRuntime(742): at android.os.Looper.loop(Looper.java:123) 
04-05 13:51:20.373: E/AndroidRuntime(742): at android.app.ActivityThread.main(ActivityThread.java:4363) 
04-05 13:51:20.373: E/AndroidRuntime(742): at java.lang.reflect.Method.invokeNative(Native Method) 
04-05 13:51:20.373: E/AndroidRuntime(742): at java.lang.reflect.Method.invoke(Method.java:521) 
04-05 13:51:20.373: E/AndroidRuntime(742): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) 
04-05 13:51:20.373: E/AndroidRuntime(742): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 
04-05 13:51:20.373: E/AndroidRuntime(742): at dalvik.system.NativeStart.main(Native Method) 
04-05 13:51:20.373: E/AndroidRuntime(742): Caused by: android.util.AndroidRuntimeException: requestFeature() must be called before adding content 
04-05 13:51:20.373: E/AndroidRuntime(742): at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:172) 
04-05 13:51:20.373: E/AndroidRuntime(742): at weather.right.Browser.onCreate(Browser.java:23) 
04-05 13:51:20.373: E/AndroidRuntime(742): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
04-05 13:51:20.373: E/AndroidRuntime(742): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459) 
04-05 13:51:20.373: E/AndroidRuntime(742): ... 11 more 
04-05 13:51:20.404: I/dalvikvm(742): threadid=7: reacting to signal 3 
04-05 13:51:20.484: I/dalvikvm(742): Wrote stack trace to '/data/anr/traces.txt' 

我怎樣才能解決這個問題呢?

在此先感謝。

編輯:新的logcat

04-05 14:00:40.893: D/dalvikvm(783): GC freed 2724 objects/171288 bytes in 136ms 
04-05 14:00:41.044: D/AndroidRuntime(783): Shutting down VM 
04-05 14:00:41.064: W/dalvikvm(783): threadid=3: thread exiting with uncaught exception (group=0x4001b188) 
04-05 14:00:41.064: E/AndroidRuntime(783): Uncaught handler: thread main exiting due to uncaught exception 
04-05 14:00:41.074: E/AndroidRuntime(783): android.app.SuperNotCalledException: Activity {weather.right.now/weather.right.Browser} did not call through to super.onCreate() 
04-05 14:00:41.074: E/AndroidRuntime(783): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2461) 
04-05 14:00:41.074: E/AndroidRuntime(783): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512) 
04-05 14:00:41.074: E/AndroidRuntime(783): at android.app.ActivityThread.access$2200(ActivityThread.java:119) 
04-05 14:00:41.074: E/AndroidRuntime(783): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863) 
04-05 14:00:41.074: E/AndroidRuntime(783): at android.os.Handler.dispatchMessage(Handler.java:99) 
04-05 14:00:41.074: E/AndroidRuntime(783): at android.os.Looper.loop(Looper.java:123) 
04-05 14:00:41.074: E/AndroidRuntime(783): at android.app.ActivityThread.main(ActivityThread.java:4363) 
04-05 14:00:41.074: E/AndroidRuntime(783): at java.lang.reflect.Method.invokeNative(Native Method) 
04-05 14:00:41.074: E/AndroidRuntime(783): at java.lang.reflect.Method.invoke(Method.java:521) 
04-05 14:00:41.074: E/AndroidRuntime(783): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) 
04-05 14:00:41.074: E/AndroidRuntime(783): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 
04-05 14:00:41.074: E/AndroidRuntime(783): at dalvik.system.NativeStart.main(Native Method) 
04-05 14:00:41.094: I/dalvikvm(783): threadid=7: reacting to signal 3 
04-05 14:00:41.124: I/dalvikvm(783): Wrote stack trace to '/data/anr/traces.txt' 
04-05 14:00:43.176: I/Process(783): Sending signal. PID: 783 SIG: 9 
04-05 14:00:43.994: D/LocationManager(798): Constructor: service = [email protected] 

回答

1

試試這個,讓我知道發生什麼事

@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 

getWindow().requestFeature(Window.FEATURE_PROGRESS); 
    WebView webview = new WebView(this); 
    WebSettings webSettings = webview.getSettings(); 
    webSettings.setJavaScriptEnabled(true); 
    setContentView(webview); 

    webview.loadUrl("http://erik-edgren.nu/weather"); 
} 
+0

見我的編輯我的第一篇:) – Erik 2012-04-05 14:03:30

+0

對不起,我忘了把super.onCreate(savedInstanceState); onCreate()的起始行。 – user370305 2012-04-05 14:07:26

+0

非常感謝!您的答案解決了我的問題:D我會盡我所能接受您的答案 – Erik 2012-04-05 14:10:45

1

錯誤說requestFeature() must be called before adding content。試試這個(還增加調用super.onCreate):

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstancsState); 
    WebView webview = new WebView(this); 

    getWindow().requestFeature(Window.FEATURE_PROGRESS); 

    WebSettings webSettings = webview.getSettings(); 
    webSettings.setJavaScriptEnabled(true); 

    setContentView(webview); 
    webview.loadUrl("http://erik-edgren.nu/weather"); 
} 
+0

非常感謝!你的回答也解決了我的問題^^ – Erik 2012-04-05 14:12:04

相關問題