Java – Android WebView Application in the emulator : null Application Context? Medium crash

Android WebView Application in the emulator : null Application Context? Medium crash… here is a solution to the problem.

Android WebView Application in the emulator : null Application Context? Medium crash

I’m working on a very simple Android app on a Mac in AndroidStudio, and I’ve created a Nexus S-based AVD. The app compiles without problems, the emulator starts, then I get error crashes in logcat and in the app. In the emulator, an error dialog box appears with the message “Unfortunately App has stopped”

This is logcat output (error level):

10-01 13:59:27.813  14114-14114/com.company.app E/SysUtils﹕ ApplicationContext is null in ApplicationStatus
10-01 13:59:27.832  14114-14114/com.company.app E/libEGL﹕ validate_display:255 error 3008 (EGL_BAD_DISPLAY)
10-01 13:59:27.832  14114-14114/com.company.app E/libEGL﹕ validate_display:255 error 3008 (EGL_BAD_DISPLAY)
10-01 13:59:27.832  14114-14114/com.company.app E/chromium﹕ [ERROR:gl_surface_egl.cc(327)] No suitable EGL configs found.
10-01 13:59:27.832  14114-14114/com.company.app E/chromium﹕ [ERROR:gl_surface_android.cc(23)] GLSurfaceEGL::InitializeOneOff failed.
10-01 13:59:27.832  14114-14114/com.company.app E/chromium﹕ [ERROR:browser_main_loop.cc(698)] GLSurface::InitializeOneOff failed
10-01 13:59:27.854  14114-14114/com.company.app E/DataReductionProxySettingListener﹕ No DRP key due to exception:java.lang.ClassNotFoundException: com.android.webview.chromium.Drp
10-01 13:59:28.530  14114-14165/com.company.app A/chromium﹕ [FATAL:gl_surface_android.cc(58)] Check failed: kGLImplementationNone != GetGLImplementation() (0 vs. 0)
10-01 13:59:28.530  14114-14165/com.company.app A/libc﹕ Fatal signal 6 (SIGABRT), code -6 in tid 14165 (GpuThread)

I’m assuming the first error related to the ApplicationContext is the cause of other errors, but I’m not sure. I tried about 10 different ways to set the context, but the ApplicationContext error persists.

My main question: Is the ApplicationContext error the root cause of the crash? If not, what is it? Can I try how to fix it?

The full app code is for reference:

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.company.app" >
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=". MainActivity"
            android:label="@string/app_name" >
            <uses-permission android:name="android.permission.INTERNET" />
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

The main activity .java

package com.company.app;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebSettings;

public class MainActivity extends AppCompatActivity {

private String appUrl = "http://company.com/mobile/index.php";

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

WebView myWebView = (WebView) findViewById(R.id.webView);
        myWebView.setWebViewClient(new MyWebViewClient());

WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);

myWebView.loadUrl(appUrl);
    }
}

MyWebViewClient.java

package com.company.app;

import android.content.Intent;
import android.net.Uri;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MyWebViewClient extends WebViewClient {

@Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (Uri.parse(url).getHost().equals("company.com")) {
            return false;
        }
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        view.getContext().startActivity(intent);
        return true;
    }
}

Solution

Here’s the logcat output

That’s nota Java stack trace , so not what you are looking for.

I’m assuming that the first error related to ApplicationContext is the cause of the other errors, but I’m not sure about that

Here are some of the fake messages you’ll see, even from well-run apps.

If not, what is?

As Ozbek says, <a href=”https://commonsware.com/blog/2015/08/31/hey-where-did-my-permission-go.html” rel=”noreferrer noopener nofollow”>your <uses-permission > element needs to be moved outside of <application> and made a direct child of <manifest> YOUR JAVA STACK TRACE SHOULD MENTION THE LACK OF INTERNET Permission.

There may be other issues, but this is definitely a problem.

Related Problems and Solutions