2012-12-10 207 views
0

我正在嘗試創建一個簡單的Android應用程序,該應用程序可以獲取網站的源代碼。 不管怎麼說,我寫了下面的:Android應用程序崩潰,爲什麼?

WebView webView = (WebView) findViewById(R.id.webView); 
try { 
    webView.setWebViewClient(new WebViewClient());   
    InputStream input = (InputStream) new URL(url.toString()).getContent(); 
    webView.loadDataWithBaseURL("", "<html><body><p>"+input.toString()+"</p></body></html>", "text/html", Encoding.UTF_8.toString(),""); 
    //setContentView(webView); 
} catch (Exception e) { 
    Alert alert = new Alert(getApplicationContext(), 
          "Error fetching data", e.getMessage()); 
} 

下面是錯誤的:

12-11 01:37:59.234: E/AndroidRuntime(28992): FATAL EXCEPTION: main 
12-11 01:37:59.234: E/AndroidRuntime(28992): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application 
12-11 01:37:59.234: E/AndroidRuntime(28992): at android.view.ViewRootImpl.setView(ViewRootImpl.java:701) 
12-11 01:37:59.234: E/AndroidRuntime(28992): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:345) 
12-11 01:37:59.234: E/AndroidRuntime(28992): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224) 
12-11 01:37:59.234: E/AndroidRuntime(28992): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149) 
12-11 01:37:59.234: E/AndroidRuntime(28992): at android.app.Dialog.show(Dialog.java:277) 
12-11 01:37:59.234: E/AndroidRuntime(28992): at com.myprogram.myprogram.Alert.<init>(Alert.java:11) 
12-11 01:37:59.234: E/AndroidRuntime(28992): at com.myprogram.myprogram.MainActivity$1.onClick(MainActivity.java:84) 
12-11 01:37:59.234: E/AndroidRuntime(28992): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:167) 
12-11 01:37:59.234: E/AndroidRuntime(28992): at android.os.Handler.dispatchMessage(Handler.java:99) 
12-11 01:37:59.234: E/AndroidRuntime(28992): at android.os.Looper.loop(Looper.java:137) 
12-11 01:37:59.234: E/AndroidRuntime(28992): at android.app.ActivityThread.main(ActivityThread.java:4898) 
12-11 01:37:59.234: E/AndroidRuntime(28992): at java.lang.reflect.Method.invokeNative(Native Method) 
12-11 01:37:59.234: E/AndroidRuntime(28992): at java.lang.reflect.Method.invoke(Method.java:511) 
12-11 01:37:59.234: E/AndroidRuntime(28992): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006) 
12-11 01:37:59.234: E/AndroidRuntime(28992): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773) 
12-11 01:37:59.234: E/AndroidRuntime(28992): at dalvik.system.NativeStart.main(Native Method) 
12-11 01:38:21.484: I/Process(28992): Sending signal. PID: 28992 SIG: 9 
12-11 01:38:32.739: E/Trace(29922): error opening trace file: No such file or directory (2) 
12-11 01:38:32.939: I/webclipboard(29922): clipservice: [email protected] 
12-11 01:38:32.964: V/webkit(29922): BrowserFrame constructor: this=Handler (android.webkit.BrowserFrame) {42204f18} 

我似乎無法找到一個解決方案;我的問題是什麼?

+0

你是否檢查了ID,如果它是正確的嗎? – adarsh

回答

2

試一試:與其getApplicationContext()使用this

+0

我試過了,但它給了我一個錯誤... – funerr

+1

.this呢? – sztembi

+0

我已經評論過了,但代碼不會做任何事情。你有什麼線索可以解決它嗎? (網絡視圖爲空) – funerr