1
resposta

Erro : java.lang.NullPointerException: Attempt to invoke virtual method 'void android.webkit.WebView.loadUrl(java.lang.String)' on a null object reference

Estou desenvolvendo uma aplicação android que usar a webview e faz interações com o JAVA, isso sem o uso do cordova apenas com o android studio...

Preencho a webview e tudo funciona na criação, porém quando tento preencher essa webview de um local externo a função de criação aparece esse erro:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.webkit.WebView.loadUrl(java.lang.String)' on a null object reference

Esse erro acontece na linha 41:

webView.loadUrl("file:///android_asset/www/index.html");

public class maps extends AppCompatActivity implements OnMapReadyCallback, PermissionsListener {
    // Variables needed to initialize a map
    public MapboxMap mapboxMap;
    public MapView mapView;
    public WebView webView;
    private MainActivityLocationCallback callback = new MainActivityLocationCallback(this);

    @RequiresApi(api=Build.VERSION_CODES.JELLY_BEAN)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(activity_maps);

        webView= (WebView) findViewById(R.id.webviewid2);

        WebSettings webSetting = webView.getSettings();
        webSetting.setBuiltInZoomControls(false);
        webSetting.setJavaScriptEnabled(true);
        webSetting.setDomStorageEnabled(true);
        webSetting.setDatabaseEnabled(true);
        webSetting.setDatabasePath("/data/data/pkgname/databases/");
        webSetting.setAllowFileAccess(true);
        webSetting.setAllowContentAccess(true);
        webSetting.setAllowFileAccessFromFileURLs(true);
        webSetting.setAllowUniversalAccessFromFileURLs(true);
        webSetting.setJavaScriptCanOpenWindowsAutomatically(true);
        webSetting.setJavaScriptEnabled(true);
        webView.addJavascriptInterface(new maps(), "maps_functions");
        webView.setWebChromeClient(new WebChromeClient());
        webView.loadUrl("file:///android_asset/www/index_maps.html");


    }

    @RequiresApi(api=Build.VERSION_CODES.JELLY_BEAN)
    @JavascriptInterface
    public void mostrarPrevisto(){
        //Toast.makeText(this, "Medar Tema", Toast.LENGTH_LONG).show();
        Log.i("Info log","Button Clicked" );

        webView.loadUrl("file:///android_asset/www/index.html");
    }
}
1 resposta

Hey Francilan, tudo bem ?

Cara eu tentaria remover a annotation @RequiresApi(api=Build.VERSION_CODES.JELLY_BEAN) para ver se resolve o problema.