I have an rss app that opens the url in a webView at the beginning it loaded very well then it started to close

0
 @Override
    protected void onCreate(Bundle savedInstanceState) {
        webView1.getSettings().setJavaScriptEnabled(true);
        webView1.getSettings().setBuiltInZoomControls(false);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_view);
        webView1 = findViewById(R.id.webvi);
        Bundle bundle = getIntent().getExtras();
        webView1.loadUrl(bundle.getString("url"));

    }
  

FATAL EXCEPTION: main                                                                          Process: com.example.ariel.rss, PID: 10355                                                                          java.lang.RuntimeException: Unable to start activity   ComponentInfo {com.example.ariel.rss / com.example.ariel.rss.WebView}:   java.lang.NullPointerException: Attempt to invoke virtual method   'android.webkit.WebSettings android.webkit.WebView.getSettings ()' on a   null object reference                                                                              at   android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2659)                                                                              at   android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2724)                                                                              at android.app.ActivityThread.-wrap12 (ActivityThread.java)                                                                              at   android.app.ActivityThread $ H.handleMessage (ActivityThread.java:1473)                                                                              at android.os.Handler.dispatchMessage (Handler.java:102)                                                                              at android.os.Looper.loop (Looper.java:154)                                                                              at android.app.ActivityThread.main (ActivityThread.java:6123)                                                                              at java.lang.reflect.Method.invoke (Native Method)                                                                              at   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:867)                                                                              at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:757)                                                                           Caused by: java.lang.NullPointerException: Attempt to invoke virtual   method 'android.webkit.WebSettings   android.webkit.WebView.getSettings () 'on a null object reference                                                                              at com.example.ariel.rss.WebView.onCreate (WebView.java:13)                                                                              at android.app.Activity.performCreate (Activity.java:6672)                                                                              at   android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1140)                                                                              at   android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2612)                                                                              at   android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2724)                                                                              at android.app.ActivityThread.-wrap12 (ActivityThread.java)                                                                              at   android.app.ActivityThread $ H.handleMessage (ActivityThread.java:1473)                                                                              at android.os.Handler.dispatchMessage (Handler.java:102)                                                                              at android.os.Looper.loop (Looper.java:154)                                                                              at android.app.ActivityThread.main (ActivityThread.java:6123)                                                                              at java.lang.reflect.Method.invoke (Native Method)                                                                              at   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:867)                                                                              at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:757)

    
asked by arglez35 29.11.2017 в 07:08
source

1 answer

0

If you set these using the WebView methods before instantiating it. That is the origin of the error, since you use the WebView methods before creating it. And even, not only are you using them before instantiating the WebView if not before inflating the layout of the activity. The methods super() and setContentView() must be the first two methods of onCreate() .

To correct the problem, instantiate the WebView before using its methods. As follows:

@Override 
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_web_view); // Inflas el layout de la actividad

    webView1 = findViewById(R.id.webvi); // Instancias el WebView

    webView1.getSettings().setJavaScriptEnabled(true); 
    webView1.getSettings().setBuiltInZoomControls(false);   

    Bundle bundle = getIntent().getExtras(); 
    webView1.loadUrl(bundle.getString("url"));
}
    
answered by 29.11.2017 / 13:58
source