我正在尝试创建一个简单的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: android.sec.clipboard.ClipboardExManager@42209de8
12-11 01:38:32.964: V/webkit(29922): BrowserFrame constructor: this=Handler (android.webkit.BrowserFrame) {42204f18}
我似乎无法找到解决方案;我的问题是什么?
答案 0 :(得分:2)
试一试:而不是getApplicationContext()
使用this