尝试在空对象引用上调用虚拟方法 'android.webkit.WebSettings android.webkit.WebView.getSettings()'

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

我正在为我的网站创建一个应用程序,但我在编译时一直收到此错误 运行 它(它在之前没有错误地工作 !!!)

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

这是我的 onCreate 方法:

@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);
    }
}

并且我已将 WebView 添加到辅助 xml 文件而不是 activity_main 文件,因为它是一个导航抽屉应用程序,我希望该 WebView 仅在一页中。 如果有人知道如何修复它,请告诉我并谢谢 :)

您的 "activity_main" xml 布局中没有 ID 名称 "spraySite"。

所以你的 sprayWeb 是一个空对象。

拜托,post 您的 xml 文件:activity_main.xml 以及您添加 ID "spraySite" 的 webView 的文件。问题是您仍然试图从 activity_main.

获取 WebView