Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attempt to invoke virtual method 'android.webkit.WebSettings android.webkit.WebView.getSettings()' on a null object reference [duplicate]

I'm creating an application for my site but I keep getting this error when compiling and running it (It worked without errors before tho !!!)

dRuntime: FATAL EXCEPTION: main
                  Process: com.sp.omar.spray, PID: 3914
                  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sp.omar.spray/com.sp.omar.spray.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.webkit.WebSettings android.webkit.WebView.getSettings()' on a null object reference

Here's my onCreate method:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                sendEmail();
            }
        });

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        displayView(R.id.nav_home);

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();

        WebView sprayWeb = (WebView) findViewById(R.id.spraySite);
        String url = "https://spray.000webhostapp.com/spray/spray/html/index.html";

        WebSettings spraySettings = sprayWeb.getSettings();
        spraySettings.setJavaScriptEnabled(true);
        spraySettings.setBuiltInZoomControls(true);

        sprayWeb.setVerticalScrollBarEnabled(true);
        sprayWeb.setHorizontalScrollBarEnabled(true);

        sprayWeb.loadUrl(url);
    }

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

</android.support.v4.widget.DrawerLayout>

home.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <WebView
        android:id="@+id/spraySite"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>
</LinearLayout>

HomeFragment.class:

package com.sp.omar.spray;

    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;

    public class HomeFragment extends Fragment {

        public View onCreateView(LayoutInflater inflater, ViewGroup group, Bundle savedInstanceState){
            super.onCreateView(inflater, group, savedInstanceState);
            final View homeView = inflater.inflate(R.layout.home, group, false);
            Home homeActivity = new Home();
            homeActivity.onCreate(savedInstanceState);
            return homeView;
        }

    }

Home.class:

package com.sp.omar.spray;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.webkit.WebSettings;
import android.webkit.WebView;

/**
 * Created by i0mar on 12/28/2016.
 */

public class Home extends AppCompatActivity {

    private WebView sprayWeb;
    private String url;
    private WebSettings spraySettings;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home);

        sprayWeb = (WebView) findViewById(R.id.spraySite);
        url = "https://spray.000webhostapp.com/spray/spray/html/index.html";

        spraySettings = sprayWeb.getSettings();
        spraySettings.setJavaScriptEnabled(true);
        spraySettings.setBuiltInZoomControls(true);

        sprayWeb.setVerticalScrollBarEnabled(true);
        sprayWeb.setHorizontalScrollBarEnabled(true);

        sprayWeb.loadUrl(url);
    }
}

and I've added the WebView to a secondary xml file not the activity_main one cuz it's a navigation drawer app and I want that WebView to be only in one page. If anyone know how to fix it please tell me and thanks :)

like image 643
i0mar Avatar asked Oct 28 '25 08:10

i0mar


1 Answers

there is no id name "spraySite" int your "activity_main" xml layout.

so your sprayWeb is a null object.

like image 91
Thinsky Avatar answered Oct 29 '25 23:10

Thinsky



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!