java Android WebView 应用程序在模拟器中崩溃:空应用程序上下文?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32897280/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 20:56:29  来源:igfitidea点击:

Android WebView app crashing in emulator: null Application Context?

javaandroidwebview

提问by byron

I'm developing a very simple Android app on a Mac in AndroidStudio and I've created an AVD based on the Nexus S. The app compiles with no issues, the emulator launches and then I get errors in logcat and the app crashes. In the emulator, an error dialog displays with the message "Unfortunately App has stopped"

我正在 Mac 上的 AndroidStudio 中开发一个非常简单的 Android 应用程序,我已经创建了一个基于 Nexus S 的 AVD。该应用程序编译没有问题,模拟器启动,然后我在 logcat 中出现错误并且应用程序崩溃。在模拟器中,将显示一个错误对话框,并显示消息“不幸的是,应用程序已停止”

Here's the logcat output (error level):

这是 logcat 输出(错误级别):

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 that the first error related to ApplicationContext is the cause of the other errors, but I'm not sure about that. I've tried about 10 different methods for setting context, but the ApplicationContext error persists.

我假设与 ApplicationContext 相关的第一个错误是其他错误的原因,但我不确定。我尝试了大约 10 种不同的设置上下文的方法,但 ApplicationContext 错误仍然存​​在。

My main questions: Is the ApplicationContext error the root cause of the crash? If not, what is? What might I try to fix it?

我的主要问题是:ApplicationContext 错误是崩溃的根本原因吗?如果不是,那是什么?我可以尝试修复什么?

Here's the complete application code for reference:

这是完整的应用程序代码以供参考:

AndroidManifest.xml

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>

MainActivity.java

主活动.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

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

采纳答案by CommonsWare

Here's the logcat output

这是 logcat 输出

That is not a Java stack trace, and therefore is not what you're looking for.

这不是Java 堆栈跟踪,因此不是您要查找的内容。

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

我假设与 ApplicationContext 相关的第一个错误是其他错误的原因,但我不确定

That is some spurious message that you will see, even from apps that run fine.

即使从运行良好的应用程序中,您也会看到一些虚假消息。

If not, what is?

如果不是,那是什么?

As ozbek notes, your <uses-permission>element needs to be moved outside of <application>and made a direct child of <manifest>. Your Java stack trace should mention a missing INTERNETpermission.

随着乌孜别克笔记,你的<uses-permission>元素需要移动之外<application>而制成的直接孩子<manifest>。您的 Java 堆栈跟踪应该提到缺少INTERNET权限。

It is possible that there are other problems, but that's definitely one.

可能还有其他问题,但这绝对是一个。