3

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 :)

i0mar
  • 105
  • 3
  • 10
  • post your xml code – sasikumar Dec 28 '16 at 09:36
  • A webview with "spraySite" as id should be there in your "activity_main".You have defined it in some other layout.And thus you are getting null oblect for "sprayWeb" – Sunil Sunny Dec 28 '16 at 09:39
  • @i0mar you cannot call a view that is not connected/linked to an activity , You have a web view in your home xml . Is there any connection that you establish between your home.xml and activity ? – Charuක Dec 28 '16 at 09:50

2 Answers2

2

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

so your sprayWeb is a null object.

sasikumar
  • 12,540
  • 3
  • 28
  • 48
Thinsky
  • 4,226
  • 3
  • 13
  • 22
0

Please, post your xml files: activity_main.xml and that file where you add your webView with id "spraySite". The problem is that you still tried to get your WebView from activity_main.

Charuක
  • 12,953
  • 5
  • 50
  • 88
Hetfieldan24
  • 198
  • 11
  • Done, sry for that – i0mar Dec 28 '16 at 09:43
  • As i understand your wishes: should make two fragments: at the first put all code from activity_main.xml, at the second put all your code from home.xml. For example, HomeFragment.java and MainFragment.java. Activity_main.xml should have only container for this fragments. And in the java code you should just replace your fragments as you wish dynamically. Do you know how to do it, or need link to explanation?If you already tried this and that doesn't work, please, post all your code, your fragments and activity, java and xml, otherwise it will be difficult to understand what you're doing wrong. – Hetfieldan24 Dec 28 '16 at 10:06
  • It fixed that error but I got another one "Attempt to invoke virtual method 'android.view.Window$Callback null object reference" I'll post my HomeFragment and my Home cass rn – i0mar Dec 28 '16 at 10:55
  • I recommend you read this article and the next few after that for better understanding what you're doing and why: https://developer.android.com/training/basics/fragments/creating.html – Hetfieldan24 Dec 28 '16 at 11:06
  • Thanks, I was an idiot, it worked now :) – i0mar Jan 01 '17 at 00:12
  • Glad to help you :) – Hetfieldan24 Jan 09 '17 at 08:36