的WebView加载网址 [英] WebView loading Url

查看:105
本文介绍了的WebView加载网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在努力增加一些Web视图的东西,将最终使这更复杂,但现在我只是想获得基本的工作。我有我的XML建成这样的,我在另一个视图之上膨胀了。因此,它像一个对话框。

So I'm working on adding some web view stuff and will eventually make this more complicated but for now am just trying to get the basics working. I have my xml built and such and I am inflating it on top of another view. So its in like a dialog box.

web = (WebView)findViewById(R.id.WebView);

name = (TextView) findViewById(R.id.name);
name.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {

        final Dialog dialog = new Dialog(Hydrogen.this);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.web);
        dialog.setCancelable(true);

        web.loadUrl("http://en.wikipedia.org/wiki/" + name.getText().toString());

        RelativeLayout btn = (RelativeLayout) dialog.findViewById(R.id.close);
        btn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                dialog.dismiss();
                        }
            });     
        dialog.show();
}});

基本上这就是我关于的WebView code等。现在的问题是对管线145这是web.loadUrl线。 I'n不知道什么是错,但每次我尝试打开它crashs对话框。谢谢。

Basically thats my code regarding the WebView etc. The problem is on line 145 which is the web.loadUrl line. I'n not sure whats wrong but every time I try to open the Dialog it crashs. Thanks.

02-16 17:16:08.313: ERROR/AndroidRuntime(8122): FATAL EXCEPTION: main
02-16 17:16:08.313: ERROR/AndroidRuntime(8122): java.lang.NullPointerException
02-16 17:16:08.313: ERROR/AndroidRuntime(8122):     at table.periodic.Hydrogen$1.onClick(Hydrogen.java:145)
02-16 17:16:08.313: ERROR/AndroidRuntime(8122):     at android.view.View.performClick(View.java:2408)
02-16 17:16:08.313: ERROR/AndroidRuntime(8122):     at android.view.View$PerformClick.run(View.java:8816)
02-16 17:16:08.313: ERROR/AndroidRuntime(8122):     at android.os.Handler.handleCallback(Handler.java:587)
02-16 17:16:08.313: ERROR/AndroidRuntime(8122):     at android.os.Handler.dispatchMessage(Handler.java:92)
02-16 17:16:08.313: ERROR/AndroidRuntime(8122):     at android.os.Looper.loop(Looper.java:123)
02-16 17:16:08.313: ERROR/AndroidRuntime(8122):     at android.app.ActivityThread.main(ActivityThread.java:4627)
02-16 17:16:08.313: ERROR/AndroidRuntime(8122):     at java.lang.reflect.Method.invokeNative(Native Method)
02-16 17:16:08.313: ERROR/AndroidRuntime(8122):     at java.lang.reflect.Method.invoke(Method.java:521)
02-16 17:16:08.313: ERROR/AndroidRuntime(8122):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-16 17:16:08.313: ERROR/AndroidRuntime(8122):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-16 17:16:08.313: ERROR/AndroidRuntime(8122):     at dalvik.system.NativeStart.main(Native Method)

我也只是试图使中加载网页一个单独的项目。短短的WebView加载的URL。这部分的工作。它的工作,以它打开的URL中的点,但它做到了在浏览器中,而不是web视图里面。可能是因为我命名的WebView浏览器,但会尝试更多。

I also just tried making a seperate project in which to load the web page. Just a WebView loading the URL. It partly worked. It worked to the point that it opened the URL but it did it inside the browser instead of inside the webview. Could be because I named the WebView browser but will try more.

推荐答案

所以,我发现它自己。我的对话框code不得不看起来如下。

So I found it out myself. My dialog code had to looks as follows.

name = (TextView) findViewById(R.id.name);
    name.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            final Dialog dialog = new Dialog(Hydrogen.this);
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            dialog.setContentView(R.layout.web);
            dialog.setCancelable(true);

            eleweb = (WebView) dialog.findViewById(R.id.eleweb);
            eleweb.loadUrl("http://en.wikipedia.org/wiki/" + name.getText().toString());
            eleweb.setWebViewClient(new HelloWebViewClient());

            RelativeLayout btn = (RelativeLayout) dialog.findViewById(R.id.close);
            btn.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    dialog.dismiss();
                            }
                });     
            dialog.show();
    }});

问题是什么,我是不是要求在对话框格式的WebView。一旦我得到了所有的WebView信息加载对话框内,做了

the problem what that I wasn't calling the WebView in a dialog format. Once I got all the WebView information loading inside the Dialog and did

eleweb = (WebView) dialog.findViewById(R.id.eleweb);

它的工作。

it worked.

这篇关于的WebView加载网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆