我正在为我的网站创建一个应用程序但是在编译和运行它时我一直收到这个错误(它之前没有错误!)
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只在一个页面中。 如果有人知道如何解决它,请告诉我并谢谢:)
答案 0 :(得分:0)
“activity_main”xml布局中没有id名称“spraySite”。
所以你的sprayWeb是一个空对象。
答案 1 :(得分:0)
请发布您的xml文件:activity_main.xml
以及您添加webView
ID为&#34; spraySite&#34;的文件。问题是您仍然试图从WebView
获取activity_main
。