Currently I am working on a project(It' a browser). There are 2 layout files one is activity_main.xml
and other is url_load_activity.xml
. In activity main file there are 10 image views with android:clickable="true"
and they are with OnClickListeners
in the MainActivity.java. The other XML contains a WebView
and a LinearLayout
for search bar, home button. I added
Intent yt = new Intent(MainActivity.this, url_load_activity.class);
startActivity(yt);
these 2 lines to each OnClickListener
of the image views in MainActivity.java
for switching to url_load_activity.xml
from activity_main.xml
. It successfully switched for url_load_activity.xml
. And then I need to load a URL in the url_load_activity.xml
while switching to it but when clicked on that image view with webView.loadUrl("https://www.youtube.com");
, the application stops and restarts. In the android studio there shows this error
2021-08-30 14:34:58.781 27973-27973/com.blurredtechnology.browser E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.blurredtechnology.browser, PID: 27973
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.webkit.WebView.loadUrl(java.lang.String)' on a null object reference
at com.blurredtechnology.browser.MainActivity$3.onClick(MainActivity.java:67)
at android.view.View.performClick(View.java:7171)
at android.view.View.performClickInternal(View.java:7148)
at android.view.View.access$3500(View.java:802)
at android.view.View$PerformClick.run(View.java:27409)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7642)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:503)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
Other image views are working well without the webView.loadUrl("");
but after adding webView.loadUrl("")
to each the error shows always