gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 11:56:25 25 4
gpt4 key购买 nike

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

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

}

主页.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是一个空对象。

关于java - 尝试在空对象引用上调用虚拟方法 'android.webkit.WebSettings android.webkit.WebView.getSettings()',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41359088/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com