在 Android 的对话框中加载 webview 时出现无效 android.webkit.WebView.loadUrl?

Getting void android.webkit.WebView.loadUrl while loading webview in dialog in Android?

我收到此错误 原因:java.lang.NullPointerException:当我尝试在 android.I 中的对话框中加载 webview 时,尝试调用虚拟方法 'void android.webkit.WebView.loadUrl 已附加日志和代码,我在下面使用过。请提供您的知识来解决它。

02-06 18:07:41.605: E/AndroidRuntime(1492): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tanas.activities/com.tanas.activities.ContactInfoActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.webkit.WebView.loadUrl(java.lang.String)' on a null object reference 02-06 18:07:41.605: E/AndroidRuntime(1492): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298) 02-06 18:07:41.605: E/AndroidRuntime(1492): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 02-06 18:07:41.605: E/AndroidRuntime(1492): at android.app.ActivityThread.access0(ActivityThread.java:144) 02-06 18:07:41.605: E/AndroidRuntime(1492): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 02-06 18:07:41.605: E/AndroidRuntime(1492): at android.os.Handler.dispatchMessage(Handler.java:102) 02-06 18:07:41.605: E/AndroidRuntime(1492): at android.os.Looper.loop(Looper.java:135) 02-06 18:07:41.605: E/AndroidRuntime(1492): at android.app.ActivityThread.main(ActivityThread.java:5221) 02-06 18:07:41.605: E/AndroidRuntime(1492): at java.lang.reflect.Method.invoke(Native Method) 02-06 18:07:41.605: E/AndroidRuntime(1492): at java.lang.reflect.Method.invoke(Method.java:372) 02-06 18:07:41.605: E/AndroidRuntime(1492): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 02-06 18:07:41.605: E/AndroidRuntime(1492): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 02-06 18:07:41.605: E/AndroidRuntime(1492): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.webkit.WebView.loadUrl(java.lang.String)' on a null object reference 02-06 18:07:41.605: E/AndroidRuntime(1492): at com.tanas.activities.ContactInfoActivity.showCustomDialog(ContactInfoActivity.java:37) 02-06 18:07:41.605: E/AndroidRuntime(1492): at com.tanas.activities.ContactInfoActivity.onCreate(ContactInfoActivity.java:21) 02-06 18:07:41.605: E/AndroidRuntime(1492): at android.app.Activity.performCreate(Activity.java:5933) 02-06 18:07:41.605: E/AndroidRuntime(1492): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) 02-06 18:07:41.605: E/AndroidRuntime(1492): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251) 02-06 18:07:41.605: E/AndroidRuntime(1492): ... 10 more

密码是

m_dialog = new Dialog(ContactInfoActivity.this, R.style.Dialog_No_Border);
    m_dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);


    LayoutInflater m_inflater = LayoutInflater.from(ContactInfoActivity.this);
    View m_view = m_inflater.inflate(R.layout.activity_contact, null);
    m_llMain = (LinearLayout) m_view.findViewById(R.id.cadllMain);

    m_llMain.setBackgroundResource(R.drawable.btn_style_roundcorner);

    WebView wv = (WebView) m_dialog.findViewById(R.id.webview1);
    //WebView wv = new WebView(this);
    wv.loadUrl("http:\www.google.com");
    wv.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);

            return true;
        }
    });

    m_dialog.setContentView(m_view);
    m_dialog.show();

m_dialog 显然没有 R.id.webview1 小部件。

如果 R.id.webview1R.layout.activity_contact 中,请在 m_llMain 上调用 findViewById(),而不是 m_dialog

如果 webView1 在 R.layout.activity_contact 中,执​​行此操作..

WebView wv = (WebView) m_view.findViewById(R.id.webview1);

我认为您没有在 activity 中设置布局。试试这个

 setContentView(R.layout.activity_web_view_sample);

在您的 Activity 中的 Oncreate() 方法上添加这一行:-

    setContentView(R.layout.activity_name);