Getting void android.webkit.WebView.loadUrl while

2019-08-10 05:16发布

I am getting this error Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.webkit.WebView.loadUrl when I try to load a webview inside a dialog in android.I have attached the log and code and I have used below. Kindly provide your knowledge to solve it.

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.access$800(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

Code is

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();

3条回答
贪生不怕死
2楼-- · 2019-08-10 05:36

i think you are not setting the layout in activity. try this

 setContentView(R.layout.activity_web_view_sample);
查看更多
聊天终结者
3楼-- · 2019-08-10 05:41

If webView1 is in R.layout.activity_contact do this..

WebView wv = (WebView) m_view.findViewById(R.id.webview1);
查看更多
你好瞎i
4楼-- · 2019-08-10 05:57

m_dialog does not have a R.id.webview1 widget, apparently.

If R.id.webview1 is in R.layout.activity_contact, call findViewById() on m_llMain, not m_dialog.

查看更多
登录 后发表回答